Initial Commit (broken due to version update)
This commit is contained in:
parent
74003df84b
commit
3f7b3b7440
3132 changed files with 1103305 additions and 0 deletions
|
|
@ -0,0 +1,52 @@
|
|||
using MessagePack.Formatters;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Linq; // require UNITY_WSA
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
/// <summary>
|
||||
/// Get formatter from [MessaegPackFromatter] attribute.
|
||||
/// </summary>
|
||||
public sealed class AttributeFormatterResolver : IFormatterResolver
|
||||
{
|
||||
public static IFormatterResolver Instance = new AttributeFormatterResolver();
|
||||
|
||||
AttributeFormatterResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
#if UNITY_WSA && !NETFX_CORE
|
||||
var attr = (MessagePackFormatterAttribute)typeof(T).GetCustomAttributes(typeof(MessagePackFormatterAttribute), true).FirstOrDefault();
|
||||
#else
|
||||
var attr = typeof(T).GetTypeInfo().GetCustomAttribute<MessagePackFormatterAttribute>();
|
||||
#endif
|
||||
if (attr == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (attr.Arguments == null)
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(attr.FormatterType);
|
||||
}
|
||||
else
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(attr.FormatterType, attr.Arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 42fec6bcb9efe7e4b9926ff05781af4d
|
||||
timeCreated: 1496903961
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
151
Assets/Scripts/MessagePack/Resolvers/BuiltinResolver.cs
Normal file
151
Assets/Scripts/MessagePack/Resolvers/BuiltinResolver.cs
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using MessagePack.Resolvers;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class BuiltinResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new BuiltinResolver();
|
||||
|
||||
BuiltinResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
// Reduce IL2CPP code generate size(don't write long code in <T>)
|
||||
formatter = (IMessagePackFormatter<T>)BuiltinResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class BuiltinResolverGetFormatterHelper
|
||||
{
|
||||
static readonly Dictionary<Type, object> formatterMap = new Dictionary<Type, object>()
|
||||
{
|
||||
// Primitive
|
||||
{typeof(Int16), Int16Formatter.Instance},
|
||||
{typeof(Int32), Int32Formatter.Instance},
|
||||
{typeof(Int64), Int64Formatter.Instance},
|
||||
{typeof(UInt16), UInt16Formatter.Instance},
|
||||
{typeof(UInt32), UInt32Formatter.Instance},
|
||||
{typeof(UInt64), UInt64Formatter.Instance},
|
||||
{typeof(Single), SingleFormatter.Instance},
|
||||
{typeof(Double), DoubleFormatter.Instance},
|
||||
{typeof(bool), BooleanFormatter.Instance},
|
||||
{typeof(byte), ByteFormatter.Instance},
|
||||
{typeof(sbyte), SByteFormatter.Instance},
|
||||
{typeof(DateTime), DateTimeFormatter.Instance},
|
||||
{typeof(char), CharFormatter.Instance},
|
||||
|
||||
// Nulllable Primitive
|
||||
{typeof(Nullable<Int16>), NullableInt16Formatter.Instance},
|
||||
{typeof(Nullable<Int32>), NullableInt32Formatter.Instance},
|
||||
{typeof(Nullable<Int64>), NullableInt64Formatter.Instance},
|
||||
{typeof(Nullable<UInt16>), NullableUInt16Formatter.Instance},
|
||||
{typeof(Nullable<UInt32>), NullableUInt32Formatter.Instance},
|
||||
{typeof(Nullable<UInt64>), NullableUInt64Formatter.Instance},
|
||||
{typeof(Nullable<Single>), NullableSingleFormatter.Instance},
|
||||
{typeof(Nullable<Double>), NullableDoubleFormatter.Instance},
|
||||
{typeof(Nullable<bool>), NullableBooleanFormatter.Instance},
|
||||
{typeof(Nullable<byte>), NullableByteFormatter.Instance},
|
||||
{typeof(Nullable<sbyte>), NullableSByteFormatter.Instance},
|
||||
{typeof(Nullable<DateTime>), NullableDateTimeFormatter.Instance},
|
||||
{typeof(Nullable<char>), NullableCharFormatter.Instance},
|
||||
|
||||
// StandardClassLibraryFormatter
|
||||
{typeof(string), NullableStringFormatter.Instance},
|
||||
{typeof(decimal), DecimalFormatter.Instance},
|
||||
{typeof(decimal?), new StaticNullableFormatter<decimal>(DecimalFormatter.Instance)},
|
||||
{typeof(TimeSpan), TimeSpanFormatter.Instance},
|
||||
{typeof(TimeSpan?), new StaticNullableFormatter<TimeSpan>(TimeSpanFormatter.Instance)},
|
||||
{typeof(DateTimeOffset), DateTimeOffsetFormatter.Instance},
|
||||
{typeof(DateTimeOffset?), new StaticNullableFormatter<DateTimeOffset>(DateTimeOffsetFormatter.Instance)},
|
||||
{typeof(Guid), GuidFormatter.Instance},
|
||||
{typeof(Guid?), new StaticNullableFormatter<Guid>(GuidFormatter.Instance)},
|
||||
{typeof(Uri), UriFormatter.Instance},
|
||||
{typeof(Version), VersionFormatter.Instance},
|
||||
{typeof(StringBuilder), StringBuilderFormatter.Instance},
|
||||
{typeof(BitArray), BitArrayFormatter.Instance},
|
||||
|
||||
// special primitive
|
||||
{typeof(byte[]), ByteArrayFormatter.Instance},
|
||||
|
||||
// Nil
|
||||
{typeof(Nil), NilFormatter.Instance},
|
||||
{typeof(Nil?), NullableNilFormatter.Instance},
|
||||
|
||||
// otpmitized primitive array formatter
|
||||
{typeof(Int16[]), Int16ArrayFormatter.Instance},
|
||||
{typeof(Int32[]), Int32ArrayFormatter.Instance},
|
||||
{typeof(Int64[]), Int64ArrayFormatter.Instance},
|
||||
{typeof(UInt16[]), UInt16ArrayFormatter.Instance},
|
||||
{typeof(UInt32[]), UInt32ArrayFormatter.Instance},
|
||||
{typeof(UInt64[]), UInt64ArrayFormatter.Instance},
|
||||
{typeof(Single[]), SingleArrayFormatter.Instance},
|
||||
{typeof(Double[]), DoubleArrayFormatter.Instance},
|
||||
{typeof(Boolean[]), BooleanArrayFormatter.Instance},
|
||||
{typeof(SByte[]), SByteArrayFormatter.Instance},
|
||||
{typeof(DateTime[]), DateTimeArrayFormatter.Instance},
|
||||
{typeof(Char[]), CharArrayFormatter.Instance},
|
||||
{typeof(string[]), NullableStringArrayFormatter.Instance},
|
||||
|
||||
// well known collections
|
||||
{typeof(List<Int16>), new ListFormatter<Int16>()},
|
||||
{typeof(List<Int32>), new ListFormatter<Int32>()},
|
||||
{typeof(List<Int64>), new ListFormatter<Int64>()},
|
||||
{typeof(List<UInt16>), new ListFormatter<UInt16>()},
|
||||
{typeof(List<UInt32>), new ListFormatter<UInt32>()},
|
||||
{typeof(List<UInt64>), new ListFormatter<UInt64>()},
|
||||
{typeof(List<Single>), new ListFormatter<Single>()},
|
||||
{typeof(List<Double>), new ListFormatter<Double>()},
|
||||
{typeof(List<Boolean>), new ListFormatter<Boolean>()},
|
||||
{typeof(List<byte>), new ListFormatter<byte>()},
|
||||
{typeof(List<SByte>), new ListFormatter<SByte>()},
|
||||
{typeof(List<DateTime>), new ListFormatter<DateTime>()},
|
||||
{typeof(List<Char>), new ListFormatter<Char>()},
|
||||
{typeof(List<string>), new ListFormatter<string>()},
|
||||
|
||||
{ typeof(ArraySegment<byte>), ByteArraySegmentFormatter.Instance },
|
||||
{ typeof(ArraySegment<byte>?),new StaticNullableFormatter<ArraySegment<byte>>(ByteArraySegmentFormatter.Instance) },
|
||||
|
||||
#if NETSTANDARD
|
||||
{typeof(System.Numerics.BigInteger), BigIntegerFormatter.Instance},
|
||||
{typeof(System.Numerics.BigInteger?), new StaticNullableFormatter<System.Numerics.BigInteger>(BigIntegerFormatter.Instance)},
|
||||
{typeof(System.Numerics.Complex), ComplexFormatter.Instance},
|
||||
{typeof(System.Numerics.Complex?), new StaticNullableFormatter<System.Numerics.Complex>(ComplexFormatter.Instance)},
|
||||
{typeof(System.Threading.Tasks.Task), TaskUnitFormatter.Instance},
|
||||
#endif
|
||||
};
|
||||
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
object formatter;
|
||||
if (formatterMap.TryGetValue(t, out formatter))
|
||||
{
|
||||
return formatter;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/Resolvers/BuiltinResolver.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Resolvers/BuiltinResolver.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f285d85e6dcdb1441951a58eae0e79a8
|
||||
timeCreated: 1488099024
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
107
Assets/Scripts/MessagePack/Resolvers/CompositeResolver.cs
Normal file
107
Assets/Scripts/MessagePack/Resolvers/CompositeResolver.cs
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
using MessagePack.Formatters;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class CompositeResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly CompositeResolver Instance = new CompositeResolver();
|
||||
|
||||
static bool isFreezed = false;
|
||||
static IMessagePackFormatter[] formatters = new IMessagePackFormatter[0];
|
||||
static IFormatterResolver[] resolvers = new IFormatterResolver[0];
|
||||
|
||||
CompositeResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public static void Register(params IFormatterResolver[] resolvers)
|
||||
{
|
||||
if (isFreezed)
|
||||
{
|
||||
throw new InvalidOperationException("Register must call on startup(before use GetFormatter<T>).");
|
||||
}
|
||||
|
||||
CompositeResolver.resolvers = resolvers;
|
||||
}
|
||||
|
||||
public static void Register(params IMessagePackFormatter[] formatters)
|
||||
{
|
||||
if (isFreezed)
|
||||
{
|
||||
throw new InvalidOperationException("Register must call on startup(before use GetFormatter<T>).");
|
||||
}
|
||||
|
||||
CompositeResolver.formatters = formatters;
|
||||
}
|
||||
|
||||
public static void Register(IMessagePackFormatter[] formatters, IFormatterResolver[] resolvers)
|
||||
{
|
||||
if (isFreezed)
|
||||
{
|
||||
throw new InvalidOperationException("Register must call on startup(before use GetFormatter<T>).");
|
||||
}
|
||||
|
||||
CompositeResolver.resolvers = resolvers;
|
||||
CompositeResolver.formatters = formatters;
|
||||
}
|
||||
|
||||
public static void RegisterAndSetAsDefault(params IFormatterResolver[] resolvers)
|
||||
{
|
||||
Register(resolvers);
|
||||
MessagePack.MessagePackSerializer.SetDefaultResolver(CompositeResolver.Instance);
|
||||
}
|
||||
|
||||
public static void RegisterAndSetAsDefault(params IMessagePackFormatter[] formatters)
|
||||
{
|
||||
Register(formatters);
|
||||
MessagePack.MessagePackSerializer.SetDefaultResolver(CompositeResolver.Instance);
|
||||
}
|
||||
|
||||
public static void RegisterAndSetAsDefault(IMessagePackFormatter[] formatters, IFormatterResolver[] resolvers)
|
||||
{
|
||||
Register(formatters);
|
||||
Register(resolvers);
|
||||
MessagePack.MessagePackSerializer.SetDefaultResolver(CompositeResolver.Instance);
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
isFreezed = true;
|
||||
|
||||
foreach (var item in formatters)
|
||||
{
|
||||
foreach (var implInterface in item.GetType().GetTypeInfo().ImplementedInterfaces)
|
||||
{
|
||||
var ti = implInterface.GetTypeInfo();
|
||||
if (ti.IsGenericType && ti.GenericTypeArguments[0] == typeof(T))
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)item;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in resolvers)
|
||||
{
|
||||
var f = item.GetFormatter<T>();
|
||||
if (f != null)
|
||||
{
|
||||
formatter = f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3705453cd664a4c469843dadb56756d7
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
#if !UNITY_WSA
|
||||
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class DynamicEnumAsStringResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new DynamicEnumAsStringResolver();
|
||||
|
||||
DynamicEnumAsStringResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
var ti = typeof(T).GetTypeInfo();
|
||||
|
||||
if (ti.IsNullable())
|
||||
{
|
||||
// build underlying type and use wrapped formatter.
|
||||
ti = ti.GenericTypeArguments[0].GetTypeInfo();
|
||||
if (!ti.IsEnum)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var innerFormatter = DynamicEnumAsStringResolver.Instance.GetFormatterDynamic(ti.AsType());
|
||||
if (innerFormatter == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter });
|
||||
return;
|
||||
}
|
||||
else if (!ti.IsEnum)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
formatter = (IMessagePackFormatter<T>)(object)new EnumAsStringFormatter<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6b1d4a5c92a16e2448c14d72a43cfa8b
|
||||
timeCreated: 1489559016
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
122
Assets/Scripts/MessagePack/Resolvers/DynamicEnumResolver.cs
Normal file
122
Assets/Scripts/MessagePack/Resolvers/DynamicEnumResolver.cs
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#if !UNITY_WSA
|
||||
|
||||
using System;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Threading;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
/// <summary>
|
||||
/// EnumResolver by dynamic code generation, serialized underlying type.
|
||||
/// </summary>
|
||||
public sealed class DynamicEnumResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly DynamicEnumResolver Instance = new DynamicEnumResolver();
|
||||
|
||||
const string ModuleName = "MessagePack.Resolvers.DynamicEnumResolver";
|
||||
|
||||
static readonly DynamicAssembly assembly;
|
||||
|
||||
static int nameSequence = 0;
|
||||
|
||||
DynamicEnumResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static DynamicEnumResolver()
|
||||
{
|
||||
assembly = new DynamicAssembly(ModuleName);
|
||||
}
|
||||
|
||||
|
||||
#if NET_35
|
||||
public AssemblyBuilder Save()
|
||||
{
|
||||
return assembly.Save();
|
||||
}
|
||||
#endif
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
var ti = typeof(T).GetTypeInfo();
|
||||
if (ti.IsNullable())
|
||||
{
|
||||
// build underlying type and use wrapped formatter.
|
||||
ti = ti.GenericTypeArguments[0].GetTypeInfo();
|
||||
if (!ti.IsEnum)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var innerFormatter = DynamicEnumResolver.Instance.GetFormatterDynamic(ti.AsType());
|
||||
if (innerFormatter == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter });
|
||||
return;
|
||||
}
|
||||
else if (!ti.IsEnum)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var formatterTypeInfo = BuildType(typeof(T));
|
||||
formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(formatterTypeInfo.AsType());
|
||||
}
|
||||
}
|
||||
|
||||
static TypeInfo BuildType(Type enumType)
|
||||
{
|
||||
var underlyingType = Enum.GetUnderlyingType(enumType);
|
||||
var formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(enumType);
|
||||
|
||||
var typeBuilder = assembly.ModuleBuilder.DefineType("MessagePack.Formatters." + enumType.FullName.Replace(".", "_") + "Formatter" + Interlocked.Increment(ref nameSequence), TypeAttributes.Public | TypeAttributes.Sealed, null, new[] { formatterType });
|
||||
|
||||
// int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver);
|
||||
{
|
||||
var method = typeBuilder.DefineMethod("Serialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
|
||||
typeof(int),
|
||||
new Type[] { typeof(byte[]).MakeByRefType(), typeof(int), enumType, typeof(IFormatterResolver) });
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
il.Emit(OpCodes.Ldarg_2);
|
||||
il.Emit(OpCodes.Ldarg_3);
|
||||
il.Emit(OpCodes.Call, typeof(MessagePackBinary).GetRuntimeMethod("Write" + underlyingType.Name, new[] { typeof(byte[]).MakeByRefType(), typeof(int), underlyingType }));
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
// T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
{
|
||||
var method = typeBuilder.DefineMethod("Deserialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
|
||||
enumType,
|
||||
new Type[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
il.Emit(OpCodes.Ldarg_2);
|
||||
il.Emit(OpCodes.Ldarg_S, (byte)4);
|
||||
il.Emit(OpCodes.Call, typeof(MessagePackBinary).GetRuntimeMethod("Read" + underlyingType.Name, new[] { typeof(byte[]), typeof(int), typeof(int).MakeByRefType() }));
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
return typeBuilder.CreateTypeInfo();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 57dbadbfd7c42564fb5db2b81393ab63
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
303
Assets/Scripts/MessagePack/Resolvers/DynamicGenericResolver.cs
Normal file
303
Assets/Scripts/MessagePack/Resolvers/DynamicGenericResolver.cs
Normal file
|
|
@ -0,0 +1,303 @@
|
|||
#if !UNITY_WSA
|
||||
|
||||
using MessagePack.Formatters;
|
||||
using System.Linq;
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections;
|
||||
|
||||
#if NETSTANDARD
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class DynamicGenericResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new DynamicGenericResolver();
|
||||
|
||||
DynamicGenericResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)DynamicGenericResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class DynamicGenericResolverGetFormatterHelper
|
||||
{
|
||||
static readonly Dictionary<Type, Type> formatterMap = new Dictionary<Type, Type>()
|
||||
{
|
||||
{typeof(List<>), typeof(ListFormatter<>)},
|
||||
{typeof(LinkedList<>), typeof(LinkedListFormatter<>)},
|
||||
{typeof(Queue<>), typeof(QeueueFormatter<>)},
|
||||
{typeof(Stack<>), typeof(StackFormatter<>)},
|
||||
{typeof(HashSet<>), typeof(HashSetFormatter<>)},
|
||||
{typeof(ReadOnlyCollection<>), typeof(ReadOnlyCollectionFormatter<>)},
|
||||
{typeof(IList<>), typeof(InterfaceListFormatter<>)},
|
||||
{typeof(ICollection<>), typeof(InterfaceCollectionFormatter<>)},
|
||||
{typeof(IEnumerable<>), typeof(InterfaceEnumerableFormatter<>)},
|
||||
{typeof(Dictionary<,>), typeof(DictionaryFormatter<,>)},
|
||||
{typeof(IDictionary<,>), typeof(InterfaceDictionaryFormatter<,>)},
|
||||
{typeof(SortedDictionary<,>), typeof(SortedDictionaryFormatter<,>)},
|
||||
{typeof(SortedList<,>), typeof(SortedListFormatter<,>)},
|
||||
{typeof(ILookup<,>), typeof(InterfaceLookupFormatter<,>)},
|
||||
{typeof(IGrouping<,>), typeof(InterfaceGroupingFormatter<,>)},
|
||||
#if NETSTANDARD
|
||||
{typeof(ObservableCollection<>), typeof(ObservableCollectionFormatter<>)},
|
||||
{typeof(ReadOnlyObservableCollection<>),(typeof(ReadOnlyObservableCollectionFormatter<>))},
|
||||
{typeof(IReadOnlyList<>), typeof(InterfaceReadOnlyListFormatter<>)},
|
||||
{typeof(IReadOnlyCollection<>), typeof(InterfaceReadOnlyCollectionFormatter<>)},
|
||||
{typeof(ISet<>), typeof(InterfaceSetFormatter<>)},
|
||||
{typeof(System.Collections.Concurrent.ConcurrentBag<>), typeof(ConcurrentBagFormatter<>)},
|
||||
{typeof(System.Collections.Concurrent.ConcurrentQueue<>), typeof(ConcurrentQueueFormatter<>)},
|
||||
{typeof(System.Collections.Concurrent.ConcurrentStack<>), typeof(ConcurrentStackFormatter<>)},
|
||||
{typeof(ReadOnlyDictionary<,>), typeof(ReadOnlyDictionaryFormatter<,>)},
|
||||
{typeof(IReadOnlyDictionary<,>), typeof(InterfaceReadOnlyDictionaryFormatter<,>)},
|
||||
{typeof(System.Collections.Concurrent.ConcurrentDictionary<,>), typeof(ConcurrentDictionaryFormatter<,>)},
|
||||
{typeof(Lazy<>), typeof(LazyFormatter<>)},
|
||||
{typeof(Task<>), typeof(TaskValueFormatter<>)},
|
||||
#endif
|
||||
};
|
||||
|
||||
// Reduce IL2CPP code generate size(don't write long code in <T>)
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
var ti = t.GetTypeInfo();
|
||||
|
||||
if (t.IsArray)
|
||||
{
|
||||
var rank = t.GetArrayRank();
|
||||
if (rank == 1)
|
||||
{
|
||||
if (t.GetElementType() == typeof(byte[])) // byte[] is also supported in builtin formatter.
|
||||
{
|
||||
return ByteArrayFormatter.Instance;
|
||||
}
|
||||
|
||||
return Activator.CreateInstance(typeof(ArrayFormatter<>).MakeGenericType(t.GetElementType()));
|
||||
}
|
||||
else if (rank == 2)
|
||||
{
|
||||
return Activator.CreateInstance(typeof(TwoDimentionalArrayFormatter<>).MakeGenericType(t.GetElementType()));
|
||||
}
|
||||
else if (rank == 3)
|
||||
{
|
||||
return Activator.CreateInstance(typeof(ThreeDimentionalArrayFormatter<>).MakeGenericType(t.GetElementType()));
|
||||
}
|
||||
else if (rank == 4)
|
||||
{
|
||||
return Activator.CreateInstance(typeof(FourDimentionalArrayFormatter<>).MakeGenericType(t.GetElementType()));
|
||||
}
|
||||
else
|
||||
{
|
||||
return null; // not supported built-in
|
||||
}
|
||||
}
|
||||
else if (ti.IsGenericType)
|
||||
{
|
||||
var genericType = ti.GetGenericTypeDefinition();
|
||||
var genericTypeInfo = genericType.GetTypeInfo();
|
||||
var isNullable = genericTypeInfo.IsNullable();
|
||||
var nullableElementType = isNullable ? ti.GenericTypeArguments[0] : null;
|
||||
|
||||
if (genericType == typeof(KeyValuePair<,>))
|
||||
{
|
||||
return CreateInstance(typeof(KeyValuePairFormatter<,>), ti.GenericTypeArguments);
|
||||
}
|
||||
else if (isNullable && nullableElementType.GetTypeInfo().IsConstructedGenericType() && nullableElementType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
|
||||
{
|
||||
return CreateInstance(typeof(NullableFormatter<>), new[] { nullableElementType });
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
|
||||
// ValueTask
|
||||
else if (genericType == typeof(ValueTask<>))
|
||||
{
|
||||
return CreateInstance(typeof(ValueTaskFormatter<>), ti.GenericTypeArguments);
|
||||
}
|
||||
else if (isNullable && nullableElementType.IsConstructedGenericType && nullableElementType.GetGenericTypeDefinition() == typeof(ValueTask<>))
|
||||
{
|
||||
return CreateInstance(typeof(NullableFormatter<>), new[] { nullableElementType });
|
||||
}
|
||||
|
||||
// Tuple
|
||||
else if (ti.FullName.StartsWith("System.Tuple"))
|
||||
{
|
||||
Type tupleFormatterType = null;
|
||||
switch (ti.GenericTypeArguments.Length)
|
||||
{
|
||||
case 1:
|
||||
tupleFormatterType = typeof(TupleFormatter<>);
|
||||
break;
|
||||
case 2:
|
||||
tupleFormatterType = typeof(TupleFormatter<,>);
|
||||
break;
|
||||
case 3:
|
||||
tupleFormatterType = typeof(TupleFormatter<,,>);
|
||||
break;
|
||||
case 4:
|
||||
tupleFormatterType = typeof(TupleFormatter<,,,>);
|
||||
break;
|
||||
case 5:
|
||||
tupleFormatterType = typeof(TupleFormatter<,,,,>);
|
||||
break;
|
||||
case 6:
|
||||
tupleFormatterType = typeof(TupleFormatter<,,,,,>);
|
||||
break;
|
||||
case 7:
|
||||
tupleFormatterType = typeof(TupleFormatter<,,,,,,>);
|
||||
break;
|
||||
case 8:
|
||||
tupleFormatterType = typeof(TupleFormatter<,,,,,,,>);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return CreateInstance(tupleFormatterType, ti.GenericTypeArguments);
|
||||
}
|
||||
|
||||
// ValueTuple
|
||||
else if (ti.FullName.StartsWith("System.ValueTuple"))
|
||||
{
|
||||
Type tupleFormatterType = null;
|
||||
switch (ti.GenericTypeArguments.Length)
|
||||
{
|
||||
case 1:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<>);
|
||||
break;
|
||||
case 2:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<,>);
|
||||
break;
|
||||
case 3:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<,,>);
|
||||
break;
|
||||
case 4:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<,,,>);
|
||||
break;
|
||||
case 5:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<,,,,>);
|
||||
break;
|
||||
case 6:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,>);
|
||||
break;
|
||||
case 7:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,,>);
|
||||
break;
|
||||
case 8:
|
||||
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,,,>);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return CreateInstance(tupleFormatterType, ti.GenericTypeArguments);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// ArraySegement
|
||||
else if (genericType == typeof(ArraySegment<>))
|
||||
{
|
||||
if (ti.GenericTypeArguments[0] == typeof(byte))
|
||||
{
|
||||
return ByteArraySegmentFormatter.Instance;
|
||||
}
|
||||
else
|
||||
{
|
||||
return CreateInstance(typeof(ArraySegmentFormatter<>), ti.GenericTypeArguments);
|
||||
}
|
||||
}
|
||||
else if (isNullable && nullableElementType.GetTypeInfo().IsConstructedGenericType() && nullableElementType.GetGenericTypeDefinition() == typeof(ArraySegment<>))
|
||||
{
|
||||
if (nullableElementType == typeof(ArraySegment<byte>))
|
||||
{
|
||||
return new StaticNullableFormatter<ArraySegment<byte>>(ByteArraySegmentFormatter.Instance);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CreateInstance(typeof(NullableFormatter<>), new[] { nullableElementType });
|
||||
}
|
||||
}
|
||||
|
||||
// Mapped formatter
|
||||
else
|
||||
{
|
||||
Type formatterType;
|
||||
if (formatterMap.TryGetValue(genericType, out formatterType))
|
||||
{
|
||||
return CreateInstance(formatterType, ti.GenericTypeArguments);
|
||||
}
|
||||
|
||||
// generic collection
|
||||
else if (ti.GenericTypeArguments.Length == 1
|
||||
&& ti.ImplementedInterfaces.Any(x => x.GetTypeInfo().IsConstructedGenericType() && x.GetGenericTypeDefinition() == typeof(ICollection<>))
|
||||
&& ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
|
||||
{
|
||||
var elemType = ti.GenericTypeArguments[0];
|
||||
return CreateInstance(typeof(GenericCollectionFormatter<,>), new[] { elemType, t });
|
||||
}
|
||||
// generic dictionary
|
||||
else if (ti.GenericTypeArguments.Length == 2
|
||||
&& ti.ImplementedInterfaces.Any(x => x.GetTypeInfo().IsConstructedGenericType() && x.GetGenericTypeDefinition() == typeof(IDictionary<,>))
|
||||
&& ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
|
||||
{
|
||||
var keyType = ti.GenericTypeArguments[0];
|
||||
var valueType = ti.GenericTypeArguments[1];
|
||||
return CreateInstance(typeof(GenericDictionaryFormatter<,,>), new[] { keyType, valueType, t });
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// NonGeneric Collection
|
||||
if (t == typeof(IList))
|
||||
{
|
||||
return NonGenericInterfaceListFormatter.Instance;
|
||||
}
|
||||
else if (t == typeof(IDictionary))
|
||||
{
|
||||
return NonGenericInterfaceDictionaryFormatter.Instance;
|
||||
}
|
||||
if (typeof(IList).GetTypeInfo().IsAssignableFrom(ti) && ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
|
||||
{
|
||||
return Activator.CreateInstance(typeof(NonGenericListFormatter<>).MakeGenericType(t));
|
||||
}
|
||||
else if (typeof(IDictionary).GetTypeInfo().IsAssignableFrom(ti) && ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
|
||||
{
|
||||
return Activator.CreateInstance(typeof(NonGenericDictionaryFormatter<>).MakeGenericType(t));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static object CreateInstance(Type genericType, Type[] genericTypeArguments, params object[] arguments)
|
||||
{
|
||||
return Activator.CreateInstance(genericType.MakeGenericType(genericTypeArguments), arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 66612a644fe399b4ab334c4e0b88ad6b
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1788
Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs
Normal file
1788
Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7eab6f6294644784f8d124742fe95896
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
515
Assets/Scripts/MessagePack/Resolvers/DynamicUnionResolver.cs
Normal file
515
Assets/Scripts/MessagePack/Resolvers/DynamicUnionResolver.cs
Normal file
|
|
@ -0,0 +1,515 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
#if !UNITY_WSA
|
||||
|
||||
/// <summary>
|
||||
/// UnionResolver by dynamic code generation.
|
||||
/// </summary>
|
||||
public sealed class DynamicUnionResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly DynamicUnionResolver Instance = new DynamicUnionResolver();
|
||||
|
||||
const string ModuleName = "MessagePack.Resolvers.DynamicUnionResolver";
|
||||
|
||||
static readonly DynamicAssembly assembly;
|
||||
#if NETSTANDARD
|
||||
static readonly Regex SubtractFullNameRegex = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+", RegexOptions.Compiled);
|
||||
#else
|
||||
static readonly Regex SubtractFullNameRegex = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+");
|
||||
#endif
|
||||
|
||||
static int nameSequence = 0;
|
||||
|
||||
DynamicUnionResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static DynamicUnionResolver()
|
||||
{
|
||||
assembly = new DynamicAssembly(ModuleName);
|
||||
}
|
||||
|
||||
#if NET_35
|
||||
public AssemblyBuilder Save()
|
||||
{
|
||||
return assembly.Save();
|
||||
}
|
||||
#endif
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
var ti = typeof(T).GetTypeInfo();
|
||||
if (ti.IsNullable())
|
||||
{
|
||||
ti = ti.GenericTypeArguments[0].GetTypeInfo();
|
||||
|
||||
var innerFormatter = DynamicUnionResolver.Instance.GetFormatterDynamic(ti.AsType());
|
||||
if (innerFormatter == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter });
|
||||
return;
|
||||
}
|
||||
|
||||
var formatterTypeInfo = BuildType(typeof(T));
|
||||
if (formatterTypeInfo == null) return;
|
||||
|
||||
formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(formatterTypeInfo.AsType());
|
||||
}
|
||||
}
|
||||
|
||||
static TypeInfo BuildType(Type type)
|
||||
{
|
||||
var ti = type.GetTypeInfo();
|
||||
// order by key(important for use jump-table of switch)
|
||||
var unionAttrs = ti.GetCustomAttributes<UnionAttribute>().OrderBy(x => x.Key).ToArray();
|
||||
|
||||
if (unionAttrs.Length == 0) return null;
|
||||
if (!ti.IsInterface && !ti.IsAbstract)
|
||||
{
|
||||
throw new MessagePackDynamicUnionResolverException("Union can only be interface or abstract class. Type:" + type.Name);
|
||||
}
|
||||
|
||||
var checker1 = new HashSet<int>();
|
||||
var checker2 = new HashSet<Type>();
|
||||
foreach (var item in unionAttrs)
|
||||
{
|
||||
if (!checker1.Add(item.Key)) throw new MessagePackDynamicUnionResolverException("Same union key has found. Type:" + type.Name + " Key:" + item.Key);
|
||||
if (!checker2.Add(item.SubType)) throw new MessagePackDynamicUnionResolverException("Same union subType has found. Type:" + type.Name + " SubType: " + item.SubType);
|
||||
}
|
||||
|
||||
var formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(type);
|
||||
var typeBuilder = assembly.ModuleBuilder.DefineType("MessagePack.Formatters." + SubtractFullNameRegex.Replace(type.FullName, "").Replace(".", "_") + "Formatter" + +Interlocked.Increment(ref nameSequence), TypeAttributes.Public | TypeAttributes.Sealed, null, new[] { formatterType });
|
||||
|
||||
FieldBuilder typeToKeyAndJumpMap = null; // Dictionary<RuntimeTypeHandle, KeyValuePair<int, int>>
|
||||
FieldBuilder keyToJumpMap = null; // Dictionary<int, int>
|
||||
|
||||
// create map dictionary
|
||||
{
|
||||
var method = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
|
||||
typeToKeyAndJumpMap = typeBuilder.DefineField("typeToKeyAndJumpMap", typeof(Dictionary<RuntimeTypeHandle, KeyValuePair<int, int>>), FieldAttributes.Private | FieldAttributes.InitOnly);
|
||||
keyToJumpMap = typeBuilder.DefineField("keyToJumpMap", typeof(Dictionary<int, int>), FieldAttributes.Private | FieldAttributes.InitOnly);
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
BuildConstructor(type, unionAttrs, method, typeToKeyAndJumpMap, keyToJumpMap, il);
|
||||
}
|
||||
|
||||
{
|
||||
var method = typeBuilder.DefineMethod("Serialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
|
||||
typeof(int),
|
||||
new Type[] { typeof(byte[]).MakeByRefType(), typeof(int), type, typeof(IFormatterResolver) });
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
BuildSerialize(type, unionAttrs, method, typeToKeyAndJumpMap, il);
|
||||
}
|
||||
{
|
||||
var method = typeBuilder.DefineMethod("Deserialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
|
||||
type,
|
||||
new Type[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
BuildDeserialize(type, unionAttrs, method, keyToJumpMap, il);
|
||||
}
|
||||
|
||||
return typeBuilder.CreateTypeInfo();
|
||||
}
|
||||
|
||||
static void BuildConstructor(Type type, UnionAttribute[] infos, ConstructorInfo method, FieldBuilder typeToKeyAndJumpMap, FieldBuilder keyToJumpMap, ILGenerator il)
|
||||
{
|
||||
il.EmitLdarg(0);
|
||||
il.Emit(OpCodes.Call, objectCtor);
|
||||
|
||||
{
|
||||
il.EmitLdarg(0);
|
||||
il.EmitLdc_I4(infos.Length);
|
||||
il.Emit(OpCodes.Ldsfld, runtimeTypeHandleEqualityComparer);
|
||||
il.Emit(OpCodes.Newobj, typeMapDictionaryConstructor);
|
||||
|
||||
var index = 0;
|
||||
foreach (var item in infos)
|
||||
{
|
||||
il.Emit(OpCodes.Dup);
|
||||
il.Emit(OpCodes.Ldtoken, item.SubType);
|
||||
il.EmitLdc_I4(item.Key);
|
||||
il.EmitLdc_I4(index);
|
||||
il.Emit(OpCodes.Newobj, intIntKeyValuePairConstructor);
|
||||
il.EmitCall(typeMapDictionaryAdd);
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
il.Emit(OpCodes.Stfld, typeToKeyAndJumpMap);
|
||||
}
|
||||
{
|
||||
il.EmitLdarg(0);
|
||||
il.EmitLdc_I4(infos.Length);
|
||||
il.Emit(OpCodes.Newobj, keyMapDictionaryConstructor);
|
||||
|
||||
var index = 0;
|
||||
foreach (var item in infos)
|
||||
{
|
||||
il.Emit(OpCodes.Dup);
|
||||
il.EmitLdc_I4(item.Key);
|
||||
il.EmitLdc_I4(index);
|
||||
il.EmitCall(keyMapDictionaryAdd);
|
||||
|
||||
index++;
|
||||
}
|
||||
il.Emit(OpCodes.Stfld, keyToJumpMap);
|
||||
}
|
||||
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
|
||||
// int Serialize([arg:1]ref byte[] bytes, [arg:2]int offset, [arg:3]T value, [arg:4]IFormatterResolver formatterResolver);
|
||||
static void BuildSerialize(Type type, UnionAttribute[] infos, MethodBuilder method, FieldBuilder typeToKeyAndJumpMap, ILGenerator il)
|
||||
{
|
||||
// if(value == null) return WriteNil
|
||||
var elseBody = il.DefineLabel();
|
||||
var notFoundType = il.DefineLabel();
|
||||
|
||||
il.EmitLdarg(3);
|
||||
il.Emit(OpCodes.Brtrue_S, elseBody);
|
||||
il.Emit(OpCodes.Br, notFoundType);
|
||||
il.MarkLabel(elseBody);
|
||||
|
||||
var keyPair = il.DeclareLocal(typeof(KeyValuePair<int, int>));
|
||||
|
||||
il.EmitLoadThis();
|
||||
il.EmitLdfld(typeToKeyAndJumpMap);
|
||||
il.EmitLdarg(3);
|
||||
il.EmitCall(objectGetType);
|
||||
il.EmitCall(getTypeHandle);
|
||||
il.EmitLdloca(keyPair);
|
||||
il.EmitCall(typeMapDictionaryTryGetValue);
|
||||
il.Emit(OpCodes.Brfalse, notFoundType);
|
||||
|
||||
// var startOffset = offset;
|
||||
var startOffsetLocal = il.DeclareLocal(typeof(int));
|
||||
il.EmitLdarg(2);
|
||||
il.EmitStloc(startOffsetLocal);
|
||||
|
||||
// offset += WriteFixedArrayHeaderUnsafe(,,2);
|
||||
EmitOffsetPlusEqual(il, null, () =>
|
||||
{
|
||||
il.EmitLdc_I4(2);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.WriteFixedArrayHeaderUnsafe);
|
||||
});
|
||||
|
||||
// offset += WriteInt32(,,keyPair.Key)
|
||||
EmitOffsetPlusEqual(il, null, () =>
|
||||
{
|
||||
il.EmitLdloca(keyPair);
|
||||
il.EmitCall(intIntKeyValuePairGetKey);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.WriteInt32);
|
||||
});
|
||||
|
||||
var loopEnd = il.DefineLabel();
|
||||
|
||||
// switch-case (offset += resolver.GetFormatter.Serialize(with cast)
|
||||
var switchLabels = infos.Select(x => new { Label = il.DefineLabel(), Attr = x }).ToArray();
|
||||
il.EmitLdloca(keyPair);
|
||||
il.EmitCall(intIntKeyValuePairGetValue);
|
||||
il.Emit(OpCodes.Switch, switchLabels.Select(x => x.Label).ToArray());
|
||||
il.Emit(OpCodes.Br, loopEnd); // default
|
||||
|
||||
foreach (var item in switchLabels)
|
||||
{
|
||||
il.MarkLabel(item.Label);
|
||||
EmitOffsetPlusEqual(il, () =>
|
||||
{
|
||||
il.EmitLdarg(4);
|
||||
il.Emit(OpCodes.Call, getFormatterWithVerify.MakeGenericMethod(item.Attr.SubType));
|
||||
}, () =>
|
||||
{
|
||||
il.EmitLdarg(3);
|
||||
if (item.Attr.SubType.GetTypeInfo().IsValueType)
|
||||
{
|
||||
il.Emit(OpCodes.Unbox_Any, item.Attr.SubType);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Castclass, item.Attr.SubType);
|
||||
}
|
||||
il.EmitLdarg(4);
|
||||
il.Emit(OpCodes.Callvirt, getSerialize(item.Attr.SubType));
|
||||
});
|
||||
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
}
|
||||
|
||||
// return startOffset- offset;
|
||||
il.MarkLabel(loopEnd);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdloc(startOffsetLocal);
|
||||
il.Emit(OpCodes.Sub);
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
// else, return WriteNil
|
||||
il.MarkLabel(notFoundType);
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.WriteNil);
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
// offset += ***(ref bytes, offset....
|
||||
static void EmitOffsetPlusEqual(ILGenerator il, Action loadEmit, Action emit)
|
||||
{
|
||||
il.EmitLdarg(2);
|
||||
|
||||
if (loadEmit != null) loadEmit();
|
||||
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
|
||||
emit();
|
||||
|
||||
il.Emit(OpCodes.Add);
|
||||
il.EmitStarg(2);
|
||||
}
|
||||
|
||||
// T Deserialize([arg:1]byte[] bytes, [arg:2]int offset, [arg:3]IFormatterResolver formatterResolver, [arg:4]out int readSize);
|
||||
static void BuildDeserialize(Type type, UnionAttribute[] infos, MethodBuilder method, FieldBuilder keyToJumpMap, ILGenerator il)
|
||||
{
|
||||
// if(MessagePackBinary.IsNil) readSize = 1, return null;
|
||||
var falseLabel = il.DefineLabel();
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.IsNil);
|
||||
il.Emit(OpCodes.Brfalse_S, falseLabel);
|
||||
|
||||
il.EmitLdarg(4);
|
||||
il.EmitLdc_I4(1);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
il.Emit(OpCodes.Ldnull);
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
// read-array header and validate, ReadArrayHeader(bytes, offset, out readSize) != 2) throw;
|
||||
il.MarkLabel(falseLabel);
|
||||
var startOffset = il.DeclareLocal(typeof(int));
|
||||
il.EmitLdarg(2);
|
||||
il.EmitStloc(startOffset);
|
||||
|
||||
var rightLabel = il.DefineLabel();
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(4);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadArrayHeader);
|
||||
il.EmitLdc_I4(2);
|
||||
il.Emit(OpCodes.Beq_S, rightLabel);
|
||||
il.Emit(OpCodes.Ldstr, "Invalid Union data was detected. Type:" + type.FullName);
|
||||
il.Emit(OpCodes.Newobj, invalidOperationExceptionConstructor);
|
||||
il.Emit(OpCodes.Throw);
|
||||
|
||||
il.MarkLabel(rightLabel);
|
||||
EmitOffsetPlusReadSize(il);
|
||||
|
||||
// read key
|
||||
var key = il.DeclareLocal(typeof(int));
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(4);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadInt32);
|
||||
il.EmitStloc(key);
|
||||
EmitOffsetPlusReadSize(il);
|
||||
|
||||
// is-sequential don't need else convert key to jump-table value
|
||||
if (!IsZeroStartSequential(infos))
|
||||
{
|
||||
var endKeyMapGet = il.DefineLabel();
|
||||
il.EmitLdarg(0);
|
||||
il.EmitLdfld(keyToJumpMap);
|
||||
il.EmitLdloc(key);
|
||||
il.EmitLdloca(key);
|
||||
il.EmitCall(keyMapDictionaryTryGetValue);
|
||||
il.Emit(OpCodes.Brtrue_S, endKeyMapGet);
|
||||
il.EmitLdc_I4(-1);
|
||||
il.EmitStloc(key);
|
||||
|
||||
il.MarkLabel(endKeyMapGet);
|
||||
}
|
||||
|
||||
// switch->read
|
||||
var result = il.DeclareLocal(type);
|
||||
var loopEnd = il.DefineLabel();
|
||||
il.Emit(OpCodes.Ldnull);
|
||||
il.EmitStloc(result);
|
||||
il.Emit(OpCodes.Ldloc, key);
|
||||
|
||||
var switchLabels = infos.Select(x => new { Label = il.DefineLabel(), Attr = x }).ToArray();
|
||||
il.Emit(OpCodes.Switch, switchLabels.Select(x => x.Label).ToArray());
|
||||
|
||||
// default
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadNextBlock);
|
||||
il.Emit(OpCodes.Add);
|
||||
il.EmitStarg(2);
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
|
||||
foreach (var item in switchLabels)
|
||||
{
|
||||
il.MarkLabel(item.Label);
|
||||
il.EmitLdarg(3);
|
||||
il.EmitCall(getFormatterWithVerify.MakeGenericMethod(item.Attr.SubType));
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(3);
|
||||
il.EmitLdarg(4);
|
||||
il.EmitCall(getDeserialize(item.Attr.SubType));
|
||||
if (item.Attr.SubType.GetTypeInfo().IsValueType)
|
||||
{
|
||||
il.Emit(OpCodes.Box, item.Attr.SubType);
|
||||
}
|
||||
il.Emit(OpCodes.Stloc, result);
|
||||
EmitOffsetPlusReadSize(il);
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
}
|
||||
|
||||
il.MarkLabel(loopEnd);
|
||||
|
||||
//// finish readSize = offset - startOffset;
|
||||
il.EmitLdarg(4);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdloc(startOffset);
|
||||
il.Emit(OpCodes.Sub);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
il.Emit(OpCodes.Ldloc, result);
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
static bool IsZeroStartSequential(UnionAttribute[] infos)
|
||||
{
|
||||
for (int i = 0; i < infos.Length; i++)
|
||||
{
|
||||
if (infos[i].Key != i) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void EmitOffsetPlusReadSize(ILGenerator il)
|
||||
{
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(4);
|
||||
il.Emit(OpCodes.Ldind_I4);
|
||||
il.Emit(OpCodes.Add);
|
||||
il.EmitStarg(2);
|
||||
}
|
||||
|
||||
// EmitInfos...
|
||||
|
||||
static readonly Type refByte = typeof(byte[]).MakeByRefType();
|
||||
static readonly Type refInt = typeof(int).MakeByRefType();
|
||||
static readonly Type refKvp = typeof(KeyValuePair<int, int>).MakeByRefType();
|
||||
static readonly MethodInfo getFormatterWithVerify = typeof(FormatterResolverExtensions).GetRuntimeMethods().First(x => x.Name == "GetFormatterWithVerify");
|
||||
|
||||
static readonly Func<Type, MethodInfo> getSerialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Serialize", new[] { refByte, typeof(int), t, typeof(IFormatterResolver) });
|
||||
static readonly Func<Type, MethodInfo> getDeserialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Deserialize", new[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), refInt });
|
||||
|
||||
static readonly FieldInfo runtimeTypeHandleEqualityComparer = typeof(RuntimeTypeHandleEqualityComparer).GetRuntimeField("Default");
|
||||
static readonly ConstructorInfo intIntKeyValuePairConstructor = typeof(KeyValuePair<int, int>).GetTypeInfo().DeclaredConstructors.First(x => x.GetParameters().Length == 2);
|
||||
static readonly ConstructorInfo typeMapDictionaryConstructor = typeof(Dictionary<RuntimeTypeHandle, KeyValuePair<int, int>>).GetTypeInfo().DeclaredConstructors.First(x => { var p = x.GetParameters(); return p.Length == 2 && p[0].ParameterType == typeof(int); });
|
||||
static readonly MethodInfo typeMapDictionaryAdd = typeof(Dictionary<RuntimeTypeHandle, KeyValuePair<int, int>>).GetRuntimeMethod("Add", new[] { typeof(RuntimeTypeHandle), typeof(KeyValuePair<int, int>) });
|
||||
static readonly MethodInfo typeMapDictionaryTryGetValue = typeof(Dictionary<RuntimeTypeHandle, KeyValuePair<int, int>>).GetRuntimeMethod("TryGetValue", new[] { typeof(RuntimeTypeHandle), refKvp });
|
||||
|
||||
static readonly ConstructorInfo keyMapDictionaryConstructor = typeof(Dictionary<int, int>).GetTypeInfo().DeclaredConstructors.First(x => { var p = x.GetParameters(); return p.Length == 1 && p[0].ParameterType == typeof(int); });
|
||||
static readonly MethodInfo keyMapDictionaryAdd = typeof(Dictionary<int, int>).GetRuntimeMethod("Add", new[] { typeof(int), typeof(int) });
|
||||
static readonly MethodInfo keyMapDictionaryTryGetValue = typeof(Dictionary<int, int>).GetRuntimeMethod("TryGetValue", new[] { typeof(int), refInt });
|
||||
|
||||
static readonly MethodInfo objectGetType = typeof(object).GetRuntimeMethod("GetType", Type.EmptyTypes);
|
||||
static readonly MethodInfo getTypeHandle = typeof(Type).GetRuntimeProperty("TypeHandle").GetGetMethod();
|
||||
|
||||
static readonly MethodInfo intIntKeyValuePairGetKey = typeof(KeyValuePair<int, int>).GetRuntimeProperty("Key").GetGetMethod();
|
||||
static readonly MethodInfo intIntKeyValuePairGetValue = typeof(KeyValuePair<int, int>).GetRuntimeProperty("Value").GetGetMethod();
|
||||
|
||||
static readonly ConstructorInfo invalidOperationExceptionConstructor = typeof(System.InvalidOperationException).GetTypeInfo().DeclaredConstructors.First(x => { var p = x.GetParameters(); return p.Length == 1 && p[0].ParameterType == typeof(string); });
|
||||
static readonly ConstructorInfo objectCtor = typeof(object).GetTypeInfo().DeclaredConstructors.First(x => x.GetParameters().Length == 0);
|
||||
|
||||
static class MessagePackBinaryTypeInfo
|
||||
{
|
||||
public static TypeInfo TypeInfo = typeof(MessagePackBinary).GetTypeInfo();
|
||||
|
||||
public static MethodInfo WriteFixedMapHeaderUnsafe = typeof(MessagePackBinary).GetRuntimeMethod("WriteFixedMapHeaderUnsafe", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WriteFixedArrayHeaderUnsafe = typeof(MessagePackBinary).GetRuntimeMethod("WriteFixedArrayHeaderUnsafe", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WriteMapHeader = typeof(MessagePackBinary).GetRuntimeMethod("WriteMapHeader", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WriteArrayHeader = typeof(MessagePackBinary).GetRuntimeMethod("WriteArrayHeader", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WritePositiveFixedIntUnsafe = typeof(MessagePackBinary).GetRuntimeMethod("WritePositiveFixedIntUnsafe", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WriteInt32 = typeof(MessagePackBinary).GetRuntimeMethod("WriteInt32", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WriteBytes = typeof(MessagePackBinary).GetRuntimeMethod("WriteBytes", new[] { refByte, typeof(int), typeof(byte[]) });
|
||||
public static MethodInfo WriteNil = typeof(MessagePackBinary).GetRuntimeMethod("WriteNil", new[] { refByte, typeof(int) });
|
||||
public static MethodInfo ReadBytes = typeof(MessagePackBinary).GetRuntimeMethod("ReadBytes", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadInt32 = typeof(MessagePackBinary).GetRuntimeMethod("ReadInt32", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadString = typeof(MessagePackBinary).GetRuntimeMethod("ReadString", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo IsNil = typeof(MessagePackBinary).GetRuntimeMethod("IsNil", new[] { typeof(byte[]), typeof(int) });
|
||||
public static MethodInfo ReadNextBlock = typeof(MessagePackBinary).GetRuntimeMethod("ReadNextBlock", new[] { typeof(byte[]), typeof(int) });
|
||||
public static MethodInfo WriteStringUnsafe = typeof(MessagePackBinary).GetRuntimeMethod("WriteStringUnsafe", new[] { refByte, typeof(int), typeof(string), typeof(int) });
|
||||
|
||||
public static MethodInfo ReadArrayHeader = typeof(MessagePackBinary).GetRuntimeMethod("ReadArrayHeader", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadMapHeader = typeof(MessagePackBinary).GetRuntimeMethod("ReadMapHeader", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
|
||||
static MessagePackBinaryTypeInfo()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
// RuntimeTypeHandle can embed directly by OpCodes.Ldtoken
|
||||
// It does not implements IEquatable<T>(but GetHashCode and Equals is implemented) so needs this to avoid boxing.
|
||||
public class RuntimeTypeHandleEqualityComparer : IEqualityComparer<RuntimeTypeHandle>
|
||||
{
|
||||
public static IEqualityComparer<RuntimeTypeHandle> Default = new RuntimeTypeHandleEqualityComparer();
|
||||
|
||||
RuntimeTypeHandleEqualityComparer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool Equals(RuntimeTypeHandle x, RuntimeTypeHandle y)
|
||||
{
|
||||
return x.Equals(y);
|
||||
}
|
||||
|
||||
public int GetHashCode(RuntimeTypeHandle obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
internal class MessagePackDynamicUnionResolverException : Exception
|
||||
{
|
||||
public MessagePackDynamicUnionResolverException(string message)
|
||||
: base(message)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 83f2ef3faec448e48b6bd9c11020b315
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class NativeDateTimeResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new NativeDateTimeResolver();
|
||||
|
||||
NativeDateTimeResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)NativeDateTimeResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class NativeDateTimeResolverGetFormatterHelper
|
||||
{
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
if (t == typeof(DateTime))
|
||||
{
|
||||
return NativeDateTimeFormatter.Instance;
|
||||
}
|
||||
else if (t == typeof(DateTime?))
|
||||
{
|
||||
return new StaticNullableFormatter<DateTime>(NativeDateTimeFormatter.Instance);
|
||||
}
|
||||
else if (t == typeof(DateTime[]))
|
||||
{
|
||||
return NativeDateTimeArrayFormatter.Instance;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 43c9611286d38a0479539bd6a5f8be1b
|
||||
timeCreated: 1490454679
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
55
Assets/Scripts/MessagePack/Resolvers/OldSpecResolver.cs
Normal file
55
Assets/Scripts/MessagePack/Resolvers/OldSpecResolver.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class OldSpecResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new OldSpecResolver();
|
||||
|
||||
OldSpecResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)OldSpecResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class OldSpecResolverGetFormatterHelper
|
||||
{
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
if (t == typeof(string))
|
||||
{
|
||||
return OldSpecStringFormatter.Instance;
|
||||
}
|
||||
else if (t == typeof(string[]))
|
||||
{
|
||||
return new ArrayFormatter<string>();
|
||||
}
|
||||
else if (t == typeof(byte[]))
|
||||
{
|
||||
return OldSpecBinaryFormatter.Instance;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/Resolvers/OldSpecResolver.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Resolvers/OldSpecResolver.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 46363e307c987604ca814cf4032137c7
|
||||
timeCreated: 1490454679
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
using MessagePack.Formatters;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class PrimitiveObjectResolver : IFormatterResolver
|
||||
{
|
||||
public static IFormatterResolver Instance = new PrimitiveObjectResolver();
|
||||
|
||||
PrimitiveObjectResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
formatter = (typeof(T) == typeof(object))
|
||||
? (IMessagePackFormatter<T>)(object)PrimitiveObjectFormatter.Instance
|
||||
: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//#if NETSTANDARD
|
||||
|
||||
// /// <summary>
|
||||
// /// In `object`, when serializing resolve by concrete type and when deserializing use primitive.
|
||||
// /// </summary>
|
||||
// public sealed class DynamicObjectTypeFallbackResolver : IFormatterResolver
|
||||
// {
|
||||
// public static IFormatterResolver Instance = new DynamicObjectTypeFallbackResolver();
|
||||
|
||||
// DynamicObjectTypeFallbackResolver()
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
// public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
// {
|
||||
// return FormatterCache<T>.formatter;
|
||||
// }
|
||||
|
||||
// static class FormatterCache<T>
|
||||
// {
|
||||
// public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
// static FormatterCache()
|
||||
// {
|
||||
// formatter = (typeof(T) == typeof(object))
|
||||
// ? (IMessagePackFormatter<T>)(object)DynamicObjectTypeFallbackFormatter.Instance
|
||||
// : null;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
//#endif
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 44530b4fdf8e2c141910bb12cd991df4
|
||||
timeCreated: 1491398526
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
353
Assets/Scripts/MessagePack/Resolvers/StandardResolver.cs
Normal file
353
Assets/Scripts/MessagePack/Resolvers/StandardResolver.cs
Normal file
|
|
@ -0,0 +1,353 @@
|
|||
using MessagePack.Formatters;
|
||||
using System.Linq;
|
||||
using MessagePack.Internal;
|
||||
using MessagePack.Resolvers;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
/// <summary>
|
||||
/// Default composited resolver, builtin -> attribute -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object -> primitive.
|
||||
/// </summary>
|
||||
public sealed class StandardResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new StandardResolver();
|
||||
|
||||
#if NETSTANDARD
|
||||
public static readonly IMessagePackFormatter<object> ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(StandardResolverCore.Instance);
|
||||
#endif
|
||||
|
||||
StandardResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
if (typeof(T) == typeof(object))
|
||||
{
|
||||
// final fallback
|
||||
#if NETSTANDARD
|
||||
formatter = (IMessagePackFormatter<T>)ObjectFallbackFormatter;
|
||||
#else
|
||||
formatter = PrimitiveObjectResolver.Instance.GetFormatter<T>();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
formatter = StandardResolverCore.Instance.GetFormatter<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ContractlessStandardResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new ContractlessStandardResolver();
|
||||
|
||||
#if NETSTANDARD
|
||||
public static readonly IMessagePackFormatter<object> ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(ContractlessStandardResolverCore.Instance);
|
||||
#endif
|
||||
|
||||
ContractlessStandardResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
if (typeof(T) == typeof(object))
|
||||
{
|
||||
// final fallback
|
||||
#if NETSTANDARD
|
||||
formatter = (IMessagePackFormatter<T>)ObjectFallbackFormatter;
|
||||
#else
|
||||
formatter = PrimitiveObjectResolver.Instance.GetFormatter<T>();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
formatter = ContractlessStandardResolverCore.Instance.GetFormatter<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StandardResolverAllowPrivate : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new StandardResolverAllowPrivate();
|
||||
|
||||
#if NETSTANDARD
|
||||
public static readonly IMessagePackFormatter<object> ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(StandardResolverAllowPrivateCore.Instance);
|
||||
#endif
|
||||
|
||||
StandardResolverAllowPrivate()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
if (typeof(T) == typeof(object))
|
||||
{
|
||||
// final fallback
|
||||
#if NETSTANDARD
|
||||
formatter = (IMessagePackFormatter<T>)ObjectFallbackFormatter;
|
||||
#else
|
||||
formatter = PrimitiveObjectResolver.Instance.GetFormatter<T>();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
formatter = StandardResolverAllowPrivateCore.Instance.GetFormatter<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ContractlessStandardResolverAllowPrivate : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new ContractlessStandardResolverAllowPrivate();
|
||||
|
||||
#if NETSTANDARD
|
||||
public static readonly IMessagePackFormatter<object> ObjectFallbackFormatter = new DynamicObjectTypeFallbackFormatter(ContractlessStandardResolverAllowPrivateCore.Instance);
|
||||
#endif
|
||||
|
||||
ContractlessStandardResolverAllowPrivate()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
if (typeof(T) == typeof(object))
|
||||
{
|
||||
// final fallback
|
||||
#if NETSTANDARD
|
||||
formatter = (IMessagePackFormatter<T>)ObjectFallbackFormatter;
|
||||
#else
|
||||
formatter = PrimitiveObjectResolver.Instance.GetFormatter<T>();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
formatter = ContractlessStandardResolverAllowPrivateCore.Instance.GetFormatter<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class StandardResolverHelper
|
||||
{
|
||||
public static readonly IFormatterResolver[] DefaultResolvers = new[]
|
||||
{
|
||||
BuiltinResolver.Instance, // Try Builtin
|
||||
AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter]
|
||||
|
||||
#if !NETSTANDARD
|
||||
MessagePack.Unity.UnityResolver.Instance,
|
||||
#endif
|
||||
|
||||
#if !ENABLE_IL2CPP && !UNITY_WSA
|
||||
|
||||
DynamicEnumResolver.Instance, // Try Enum
|
||||
DynamicGenericResolver.Instance, // Try Array, Tuple, Collection
|
||||
DynamicUnionResolver.Instance, // Try Union(Interface)
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
internal sealed class StandardResolverCore : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new StandardResolverCore();
|
||||
|
||||
static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[]
|
||||
{
|
||||
#if !ENABLE_IL2CPP && !UNITY_WSA
|
||||
DynamicObjectResolver.Instance, // Try Object
|
||||
#endif
|
||||
}).ToArray();
|
||||
|
||||
StandardResolverCore()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
foreach (var item in resolvers)
|
||||
{
|
||||
var f = item.GetFormatter<T>();
|
||||
if (f != null)
|
||||
{
|
||||
formatter = f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ContractlessStandardResolverCore : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new ContractlessStandardResolverCore();
|
||||
|
||||
static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[]
|
||||
{
|
||||
#if !ENABLE_IL2CPP && !UNITY_WSA
|
||||
DynamicObjectResolver.Instance, // Try Object
|
||||
DynamicContractlessObjectResolver.Instance, // Serializes keys as strings
|
||||
#endif
|
||||
}).ToArray();
|
||||
|
||||
|
||||
ContractlessStandardResolverCore()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
foreach (var item in resolvers)
|
||||
{
|
||||
var f = item.GetFormatter<T>();
|
||||
if (f != null)
|
||||
{
|
||||
formatter = f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class StandardResolverAllowPrivateCore : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new StandardResolverAllowPrivateCore();
|
||||
|
||||
static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[]
|
||||
{
|
||||
#if !ENABLE_IL2CPP && !UNITY_WSA
|
||||
DynamicObjectResolverAllowPrivate.Instance, // Try Object
|
||||
#endif
|
||||
}).ToArray();
|
||||
|
||||
StandardResolverAllowPrivateCore()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
foreach (var item in resolvers)
|
||||
{
|
||||
var f = item.GetFormatter<T>();
|
||||
if (f != null)
|
||||
{
|
||||
formatter = f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ContractlessStandardResolverAllowPrivateCore : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new ContractlessStandardResolverAllowPrivateCore();
|
||||
|
||||
static readonly IFormatterResolver[] resolvers = StandardResolverHelper.DefaultResolvers.Concat(new IFormatterResolver[]
|
||||
{
|
||||
#if !ENABLE_IL2CPP && !UNITY_WSA
|
||||
DynamicObjectResolverAllowPrivate.Instance, // Try Object
|
||||
DynamicContractlessObjectResolverAllowPrivate.Instance, // Serializes keys as strings
|
||||
#endif
|
||||
}).ToArray();
|
||||
|
||||
|
||||
ContractlessStandardResolverAllowPrivateCore()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
foreach (var item in resolvers)
|
||||
{
|
||||
var f = item.GetFormatter<T>();
|
||||
if (f != null)
|
||||
{
|
||||
formatter = f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2f381af8868880548a0f19513111513c
|
||||
timeCreated: 1489030684
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
#if NETSTANDARD
|
||||
using MessagePack.Formatters;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
/// <summary>
|
||||
/// Embed c# type names for `object` typed fields/collection items
|
||||
/// Preserve c# DateTime timezone
|
||||
/// </summary>
|
||||
public sealed class TypelessContractlessStandardResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new TypelessContractlessStandardResolver();
|
||||
|
||||
static readonly IFormatterResolver[] resolvers = new[]
|
||||
{
|
||||
NativeDateTimeResolver.Instance, // Native c# DateTime format, preserving timezone
|
||||
BuiltinResolver.Instance, // Try Builtin
|
||||
AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter]
|
||||
#if !ENABLE_IL2CPP
|
||||
DynamicEnumResolver.Instance, // Try Enum
|
||||
DynamicGenericResolver.Instance, // Try Array, Tuple, Collection
|
||||
DynamicUnionResolver.Instance, // Try Union(Interface)
|
||||
DynamicObjectResolver.Instance, // Try Object
|
||||
#endif
|
||||
DynamicContractlessObjectResolver.Instance, // Serializes keys as strings
|
||||
TypelessObjectResolver.Instance
|
||||
};
|
||||
|
||||
TypelessContractlessStandardResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
foreach (var item in resolvers)
|
||||
{
|
||||
var f = item.GetFormatter<T>();
|
||||
if (f != null)
|
||||
{
|
||||
formatter = f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ef8d67bd12638c74f8a8afd901614e29
|
||||
timeCreated: 1498546040
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
148
Assets/Scripts/MessagePack/Resolvers/TypelessObjectResolver.cs
Normal file
148
Assets/Scripts/MessagePack/Resolvers/TypelessObjectResolver.cs
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
using System;
|
||||
using MessagePack.Formatters;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack.Internal;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
#if NETSTANDARD
|
||||
|
||||
/// <summary>
|
||||
/// Used for `object` fields/collections, ex: var arr = new object[] { 1, "a", new Model() };
|
||||
/// The runtime type of value in object field, should be covered by one of resolvers in complex/standard resolver.
|
||||
/// TypelessObjectResolver should be placed before DynamicObjectTypeFallbackResolver and PrimitiveObjectFormatter in resolvers list.
|
||||
/// Deserializer uses Namescape.TypeName, AssemblyName to get runtime type in destination app, so that combination must be present in destination app.
|
||||
/// Serialized binary is valid MessagePack binary used ext-format and custom typecode(100).
|
||||
/// Inside ext - assembly qualified type name, and serialized object
|
||||
/// </summary>
|
||||
public sealed class TypelessObjectResolver : IFormatterResolver
|
||||
{
|
||||
public static IFormatterResolver Instance = new TypelessObjectResolver();
|
||||
|
||||
TypelessObjectResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
formatter = (typeof(T) == typeof(object))
|
||||
? (IMessagePackFormatter<T>)(object)TypelessFormatter.Instance
|
||||
: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// helpers for TypelessFormatter
|
||||
|
||||
internal sealed class ForceSizePrimitiveObjectResolver : IFormatterResolver
|
||||
{
|
||||
public static IFormatterResolver Instance = new ForceSizePrimitiveObjectResolver();
|
||||
|
||||
ForceSizePrimitiveObjectResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)Helper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
|
||||
static class Helper
|
||||
{
|
||||
static readonly Dictionary<Type, object> formatterMap = new Dictionary<Type, object>()
|
||||
{
|
||||
// Primitive
|
||||
{typeof(Int16), ForceInt16BlockFormatter.Instance},
|
||||
{typeof(Int32), ForceInt32BlockFormatter.Instance},
|
||||
{typeof(Int64), ForceInt64BlockFormatter.Instance},
|
||||
{typeof(UInt16), ForceUInt16BlockFormatter.Instance},
|
||||
{typeof(UInt32), ForceUInt32BlockFormatter.Instance},
|
||||
{typeof(UInt64), ForceUInt64BlockFormatter.Instance},
|
||||
{typeof(byte), ForceByteBlockFormatter.Instance},
|
||||
{typeof(sbyte), ForceSByteBlockFormatter.Instance},
|
||||
|
||||
// Nulllable Primitive
|
||||
{typeof(Nullable<Int16>), NullableForceInt16BlockFormatter.Instance},
|
||||
{typeof(Nullable<Int32>), NullableForceInt32BlockFormatter.Instance},
|
||||
{typeof(Nullable<Int64>), NullableForceInt64BlockFormatter.Instance},
|
||||
{typeof(Nullable<UInt16>), NullableForceUInt16BlockFormatter.Instance},
|
||||
{typeof(Nullable<UInt32>), NullableForceUInt32BlockFormatter.Instance},
|
||||
{typeof(Nullable<UInt64>), NullableForceUInt64BlockFormatter.Instance},
|
||||
{typeof(Nullable<byte>), NullableForceByteBlockFormatter.Instance},
|
||||
{typeof(Nullable<sbyte>), NullableForceSByteBlockFormatter.Instance},
|
||||
|
||||
// otpmitized primitive array formatter
|
||||
{typeof(Int16[]), ForceInt16BlockArrayFormatter.Instance},
|
||||
{typeof(Int32[]), ForceInt32BlockArrayFormatter.Instance},
|
||||
{typeof(Int64[]), ForceInt64BlockArrayFormatter.Instance},
|
||||
{typeof(UInt16[]), ForceUInt16BlockArrayFormatter.Instance},
|
||||
{typeof(UInt32[]), ForceUInt32BlockArrayFormatter.Instance},
|
||||
{typeof(UInt64[]), ForceUInt64BlockArrayFormatter.Instance},
|
||||
{typeof(SByte[]), ForceSByteBlockArrayFormatter.Instance},
|
||||
};
|
||||
|
||||
public static object GetFormatter(Type type)
|
||||
{
|
||||
object formatter;
|
||||
return formatterMap.TryGetValue(type, out formatter)
|
||||
? formatter
|
||||
: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TypelessFormatterFallbackResolver : IFormatterResolver
|
||||
{
|
||||
public static IFormatterResolver Instance = new TypelessFormatterFallbackResolver();
|
||||
|
||||
static IMessagePackFormatter<object> fallbackFormatter = new DynamicObjectTypeFallbackFormatter(
|
||||
ForceSizePrimitiveObjectResolver.Instance,
|
||||
ContractlessStandardResolverCore.Instance);
|
||||
|
||||
TypelessFormatterFallbackResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
if (typeof(T) == typeof(object))
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)fallbackFormatter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1dc6f8cfe0805a348bc4f08b0b7feafe
|
||||
timeCreated: 1498546040
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
63
Assets/Scripts/MessagePack/Resolvers/UnsafeBinaryResolver.cs
Normal file
63
Assets/Scripts/MessagePack/Resolvers/UnsafeBinaryResolver.cs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class UnsafeBinaryResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new UnsafeBinaryResolver();
|
||||
|
||||
UnsafeBinaryResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
formatter = (IMessagePackFormatter<T>)UnsafeBinaryResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class UnsafeBinaryResolverGetFormatterHelper
|
||||
{
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
if (t == typeof(Guid))
|
||||
{
|
||||
return BinaryGuidFormatter.Instance;
|
||||
}
|
||||
else if (t == typeof(Guid?))
|
||||
{
|
||||
return new StaticNullableFormatter<Guid>(BinaryGuidFormatter.Instance);
|
||||
}
|
||||
else if (t == typeof(Decimal))
|
||||
{
|
||||
return BinaryDecimalFormatter.Instance;
|
||||
}
|
||||
else if (t == typeof(Decimal?))
|
||||
{
|
||||
return new StaticNullableFormatter<Decimal>(BinaryDecimalFormatter.Instance);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d291d1fb6c3a9f04f8d3ffc7ca1c1de8
|
||||
timeCreated: 1505965655
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue