From c9fad94175b6b1d5686325cb96baaacc6f424257 Mon Sep 17 00:00:00 2001 From: maxim Date: Fri, 1 Apr 2022 19:01:45 +1300 Subject: [PATCH 1/5] Fix for "System.NotImplementedException: byref delegate" in System.Linq.Expressions for AOT compilation. --- .../MessagePack/MessagePackSerializer.NonGeneric.cs | 4 ++++ .../Scripts/MessagePack/MessagePackSerializer.cs | 12 ++++++++++++ .../MessagePack/MessagePackSerializerOptions.cs | 5 +++++ .../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 281e20f31..18c52d4e4 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 @@ internal CompiledMethods(Type type) { // 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 @@ internal CompiledMethods(Type type) 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 @@ internal CompiledMethods(Type type) 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 0e0188cff..a4f902f59 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -66,6 +66,13 @@ public static void Serialize(IBufferWriter writer, T value, 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 @@ public static T Deserialize(in ReadOnlySequence byteSequence, MessagePa /// 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 ebf1ca43b..f31174557 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs @@ -29,6 +29,11 @@ public class MessagePackSerializerOptions }; #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 52b92b0e4..643a6643b 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 @@ void OnNotFound() 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 @@ void OnNotFound() { automata.EmitMatch(il, buffer, longKey, OnFoundAssignLocalVariable, OnNotFound); } +#endif il.MarkLabel(readNext); reader.EmitLdarg(); From 7a4a9ef9fbe1d8eb2b9139afa10e6d8f691ff976 Mon Sep 17 00:00:00 2001 From: maxim Date: Tue, 28 Jun 2022 13:02:50 +1200 Subject: [PATCH 2/5] Several lines of code were deleted (which were not deleted in previous commit by some unknown accident). --- .../MessagePackSerializer.NonGeneric.cs | 21 ------------------- .../MessagePackSerializerOptions.cs | 1 - 2 files changed, 22 deletions(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs index 18c52d4e4..019eb8e78 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs @@ -220,41 +220,20 @@ internal CompiledMethods(Type type) { // 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 - ParameterExpression param1 = Expression.Parameter(typeof(MessagePackWriter).MakeByRefType(), "writer"); - ParameterExpression param2 = Expression.Parameter(typeof(object), "obj"); - ParameterExpression param3 = Expression.Parameter(typeof(MessagePackSerializerOptions), "options"); - - MethodCallExpression body = Expression.Call( - null, - serialize, - param1, - ti.IsValueType ? Expression.Unbox(param2, type) : Expression.Convert(param2, type), - param3); - 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 - ParameterExpression param1 = Expression.Parameter(typeof(MessagePackReader).MakeByRefType(), "reader"); - ParameterExpression param2 = Expression.Parameter(typeof(MessagePackSerializerOptions), "options"); - UnaryExpression body = Expression.Convert(Expression.Call(null, deserialize, param1, param2), typeof(object)); - 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/MessagePackSerializerOptions.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs index f31174557..bc1557b02 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs @@ -28,7 +28,6 @@ public class MessagePackSerializerOptions "System.Management.IWbemClassObjectFreeThreaded", }; -#if !DYNAMICCODEDUMPER #if DYNAMICCODEDUMPER static readonly MessagePackSerializerOptions _standard = new MessagePackSerializerOptions(Resolvers.BuiltinResolver.Instance); From 3cd55341c145b01fb4087aa4f407995533d732c0 Mon Sep 17 00:00:00 2001 From: maxim Date: Sun, 3 Jul 2022 20:03:26 +1200 Subject: [PATCH 3/5] Changes to fixx issues to pass CI build pipeline. --- .../MessagePack/MessagePackSerializer.cs | 24 +++++++++---------- .../MessagePackSerializerOptions.cs | 6 +---- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs index f3773f700..4348e4c3d 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -71,13 +71,6 @@ public static void Serialize(IBufferWriter writer, T value, MessagePack fastWriter.Flush(); } - /// - /// Serializes a given value with the specified buffer writer. - /// - /// The buffer writer to serialize with. - /// 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; @@ -85,6 +78,13 @@ internal static void SerializeSemiGeneric(ref MessagePackWriter writer, Objec Serialize(ref writer, value, options); } + /// + /// Serializes a given value with the specified buffer writer. + /// + /// The buffer writer to serialize with. + /// The value to serialize. + /// The options. Use null to use default options. + /// Thrown when any error occurs during serialization. public static void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options = null) { options = options ?? DefaultOptions; @@ -239,11 +239,6 @@ public static T Deserialize(in ReadOnlySequence byteSequence, MessagePa /// 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; @@ -276,6 +271,11 @@ internal static Object DeserializeSemiGeneric(ref MessagePackReader reader, M } } + internal static Object DeserializeSemiGeneric(ref MessagePackReader reader, MessagePackSerializerOptions options = null) + { + return DeserializeSemiGeneric(ref reader, options); + } + /// /// Deserializes a value of a given type from a sequence of bytes. /// diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs index de2bb4b8e..a9a9ec515 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs @@ -28,11 +28,7 @@ public class MessagePackSerializerOptions "System.Management.IWbemClassObjectFreeThreaded", }; -#if DYNAMICCODEDUMPER - static readonly MessagePackSerializerOptions _standard = new MessagePackSerializerOptions(Resolvers.BuiltinResolver.Instance); - - public static MessagePackSerializerOptions Standard => _standard; -#else +#if !DYNAMICCODEDUMPER /// /// Gets a good default set of options that uses the and no compression. /// From 287c4f7a7a2d6f93d98bd143cb7578cd9ea204da Mon Sep 17 00:00:00 2001 From: maxim Date: Sun, 3 Jul 2022 20:26:50 +1200 Subject: [PATCH 4/5] Small bug fix --- .../Assets/Scripts/MessagePack/MessagePackSerializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs index 4348e4c3d..9510ea893 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -273,7 +273,7 @@ public static T Deserialize(ref MessagePackReader reader, MessagePackSerializ internal static Object DeserializeSemiGeneric(ref MessagePackReader reader, MessagePackSerializerOptions options = null) { - return DeserializeSemiGeneric(ref reader, options); + return Deserialize(ref reader, options); } /// From eb6b93bb63794566ebb4d36e37ea173782851c79 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sat, 23 Jul 2022 08:03:47 -0600 Subject: [PATCH 5/5] Revise the fix slightly --- .../MessagePackSerializer.NonGeneric.cs | 22 ++++++++++++++++--- .../MessagePack/MessagePackSerializer.cs | 12 ---------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs index 019eb8e78..bd6f9b431 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.NonGeneric.cs @@ -82,6 +82,22 @@ public static object Deserialize(Type type, ReadOnlySequence bytes, Messag return GetOrAdd(type).Deserialize_ReadOnlySequence_Options_CancellationToken.Invoke(bytes, options, cancellationToken); } + /// + /// Helper method used by reflection. + /// + private static void SerializeSemiGeneric(ref MessagePackWriter writer, object valueObject, MessagePackSerializerOptions options = null) + { + Serialize(ref writer, (T)valueObject, options); + } + + /// + /// Helper method used by reflection. + /// + private static object DeserializeSemiGeneric(ref MessagePackReader reader, MessagePackSerializerOptions options = null) + { + return Deserialize(ref reader, options); + } + private static async ValueTask DeserializeObjectAsync(Stream stream, MessagePackSerializerOptions options, CancellationToken cancellationToken) => await DeserializeAsync(stream, options, cancellationToken).ConfigureAwait(false); private static CompiledMethods GetOrAdd(Type type) @@ -219,8 +235,8 @@ internal CompiledMethods(Type type) } { - // public static void Serialize(ref MessagePackWriter writer, T obj, MessagePackSerializerOptions options) - MethodInfo serialize = GetMethod(nameof(SerializeSemiGeneric), type, new Type[] { typeof(MessagePackWriter).MakeByRefType(), typeof(Object), typeof(MessagePackSerializerOptions) }); + // private static void SerializeSemiGeneric(ref MessagePackWriter writer, object obj, MessagePackSerializerOptions options) + 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 @@ -229,7 +245,7 @@ internal CompiledMethods(Type type) } { - // public static T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) + // private static object DeserializeSemiGeneric(ref MessagePackReader reader, MessagePackSerializerOptions options) 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; }; diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs index 9510ea893..8c4a454c6 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -71,13 +71,6 @@ public static void Serialize(IBufferWriter writer, T value, MessagePack fastWriter.Flush(); } - internal static void SerializeSemiGeneric(ref MessagePackWriter writer, Object valueObject, MessagePackSerializerOptions options = null) - { - T value = (T)valueObject; - - Serialize(ref writer, value, options); - } - /// /// Serializes a given value with the specified buffer writer. /// @@ -271,11 +264,6 @@ public static T Deserialize(ref MessagePackReader reader, MessagePackSerializ } } - internal static Object DeserializeSemiGeneric(ref MessagePackReader reader, MessagePackSerializerOptions options = null) - { - return Deserialize(ref reader, options); - } - /// /// Deserializes a value of a given type from a sequence of bytes. ///