From c9fad94175b6b1d5686325cb96baaacc6f424257 Mon Sep 17 00:00:00 2001 From: maxim Date: Fri, 1 Apr 2022 19:01:45 +1300 Subject: Fix for "System.NotImplementedException: byref delegate" in System.Linq.Expressions for AOT compilation. --- .../Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs | 4 ++++ .../Assets/Scripts/MessagePack/MessagePackSerializer.cs | 12 ++++++++++++ .../Scripts/MessagePack/MessagePackSerializerOptions.cs | 5 +++++ .../Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs | 4 ++++ 4 files changed, 25 insertions(+) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs index 281e20f3..18c52d4e 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs @@ -221,6 +221,7 @@ namespace MessagePack { // public static void Serialize(ref MessagePackWriter writer, T obj, MessagePackSerializerOptions options) MethodInfo serialize = GetMethod(nameof(Serialize), type, new Type[] { typeof(MessagePackWriter).MakeByRefType(), null, typeof(MessagePackSerializerOptions) }); + MethodInfo serialize = GetMethod(nameof(SerializeSemiGeneric), type, new Type[] { typeof(MessagePackWriter).MakeByRefType(), typeof(Object), typeof(MessagePackSerializerOptions) }); #if ENABLE_IL2CPP this.Serialize_MessagePackWriter_T_Options = (ref MessagePackWriter x, object y, MessagePackSerializerOptions z) => ThrowRefStructNotSupported(); #else @@ -237,12 +238,14 @@ namespace MessagePack MessagePackWriterSerialize lambda = Expression.Lambda(body, param1, param2, param3).Compile(PreferInterpretation); this.Serialize_MessagePackWriter_T_Options = lambda; + this.Serialize_MessagePackWriter_T_Options = (MessagePackWriterSerialize)serialize.CreateDelegate(typeof(MessagePackWriterSerialize)); #endif } { // public static T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) MethodInfo deserialize = GetMethod(nameof(Deserialize), type, new Type[] { typeof(MessagePackReader).MakeByRefType(), typeof(MessagePackSerializerOptions) }); + MethodInfo deserialize = GetMethod(nameof(DeserializeSemiGeneric), type, new Type[] { typeof(MessagePackReader).MakeByRefType(), typeof(MessagePackSerializerOptions) }); #if ENABLE_IL2CPP this.Deserialize_MessagePackReader_Options = (ref MessagePackReader reader, MessagePackSerializerOptions options) => { ThrowRefStructNotSupported(); return null; }; #else @@ -252,6 +255,7 @@ namespace MessagePack MessagePackReaderDeserialize lambda = Expression.Lambda(body, param1, param2).Compile(); this.Deserialize_MessagePackReader_Options = lambda; + this.Deserialize_MessagePackReader_Options = (MessagePackReaderDeserialize)deserialize.CreateDelegate(typeof(MessagePackReaderDeserialize)); #endif } diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs index 0e0188cf..a4f902f5 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -66,6 +66,13 @@ namespace MessagePack /// The value to serialize. /// The options. Use null to use default options. /// Thrown when any error occurs during serialization. + internal static void SerializeSemiGeneric(ref MessagePackWriter writer, Object valueObject, MessagePackSerializerOptions options = null) + { + T value = (T)valueObject; + + Serialize(ref writer, value, options); + } + public static void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options = null) { options = options ?? DefaultOptions; @@ -220,6 +227,11 @@ namespace MessagePack /// The deserialized value. /// Thrown when any error occurs during deserialization. public static T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options = null) + { + return (T)DeserializeSemiGeneric(ref reader, options); + } + + internal static Object DeserializeSemiGeneric(ref MessagePackReader reader, MessagePackSerializerOptions options = null) { options = options ?? DefaultOptions; diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs index ebf1ca43..f3117455 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs @@ -29,6 +29,11 @@ namespace MessagePack }; #if !DYNAMICCODEDUMPER +#if DYNAMICCODEDUMPER + static readonly MessagePackSerializerOptions _standard = new MessagePackSerializerOptions(Resolvers.BuiltinResolver.Instance); + + public static MessagePackSerializerOptions Standard => _standard; +#else /// /// Gets a good default set of options that uses the and no compression. /// diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs index 52b92b0e..643a6643 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs @@ -1264,6 +1264,9 @@ namespace MessagePack.Internal il.Emit(OpCodes.Br, readNext); } +#if NET_STANDARD_2_0 + throw new NotImplementedException("NET_STANDARD_2_0 directive was used"); +#else if (canOverwrite) { automata.EmitMatch(il, buffer, longKey, OnFoundAssignDirect, OnNotFound); @@ -1272,6 +1275,7 @@ namespace MessagePack.Internal { automata.EmitMatch(il, buffer, longKey, OnFoundAssignLocalVariable, OnNotFound); } +#endif il.MarkLabel(readNext); reader.EmitLdarg(); -- cgit v1.2.3