As of MesseagePack for CLI 0.5, Xamarin and Unity (including Android and iOS) are supported.
This wiki page describe how to use MessagePack for CLI in their environment with some restrictions.
MesseagePack for CLI core API can run on Xamarin and Unity. In addition, you can use serializer (MessagePackSerializer<T>
) in their platform by generating them in advance.
iOS does NOT permit any runtime code generation from JIT to IL generation (OK, latest Xamarin supports ExpressionTree interpreter, but it is not so fast now). So, because MessagePack for CLI serialization strongly depends on runtime code generation (via System.Reflection.Emit
and/or System.Linq.Expression
), it must fail when serializer object generation. As of 0.5, MessagePack for CLI introduces new serializer generation stack which uses CodeDOM (System.CodeDom
) which spawns serializers as source code. By generating serializers source code in advance and building with them, you can get AOTed serializers.
MessagePack for CLI can use in form of a NuGet package.
Refer MessagePack for CLI libraryNote As of 0.5.1, you can use reflection based serializers, so you does not have to do following instruction to work with MessagePack for CLI. But it is recommended to generate serializers in advance because reflection based serializers are not so fast. Note You must use generated serializers when you serialize value types (including enums) in Unity3D.
SerializerGenerator
API in the project.For example, you can write NUnit test code to generate serializers as follows:
// using System.Linq; [TestFixture] public class SerializerGenerator { [Test] public void GenerateSerializers() { var applicationLibraryAssembly = typeof( ApplicationLibraryClass ).Assembly; SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "Example.MessagePackSerializers", OutputDirectory = "../../GeneratedSerializers", EnumSerializationMethod = EnumSerializationMethod.ByName, // You can tweak it to use ByUnderlyingValue as you like. IsRecursive = true, // Set depenendent serializers are also generated. PreferReflectionBasedSerializer = false, // Set true if you want to use reflection based collection serializer, false if you want to get generated collection serializers. SerializationMethod = SerializationMethod.Array // You tweak it to generate 'map' based serializers. }, applicationLibraryAssembly.GetTypes().Where( type => /* ...you can filter types to be serialized by their namespace, custom attributes, etc... */ ) ); } }
As you imagine, you can embed above code in MSBuild pipeline or custom build action to automate serializer code generation.
Using Generated SerializersYou must setup serialization context to use generated serializers as following:
SerializationContext
and store it as you like.SerializationContext.RegisterSerializer
repeatedly. Target serialization context is custom context created above or default context via SerializationContext.Default
. Note that you must register all serializers for object tree except some primitives or FCL(BCL) types.unzip
).If you don't like DLL style asset, you can import MessagePack for CLI source code to the assets as follows.
mpu.exe -l -o <OUTPUT_DIR>
mono mpu.exe -l -o <OUTPUT_DIR>
If you want to build "unity3d-full" source code, you must define MSGPACK_UNITY_FULL
compilation constant in project settings.
Run following command line to generate serializer source code. Note that you can specify --include
and --exclude
options to filter target types with regular expression toward their type full name (e.g. "System.DateTime").
mpu.exe -s -a -n MyProduct.MsgPackSerializers -o /path-to-asset-root/MsgPackSerializers /path/to/assembly.dll
mpu.exe -s -n MyProduct.MsgPackSerializers -o /path-to-asset-root/MsgPackSerializers /path/to/sources/Some1.cs /path/to/sources/Some2.cs ...
When you import MsgPack as source code style instead of dll (assembly) style, you also specify --internal
option for the mpu.exe
.
Some time, the Mono runtime throws ExecutionEngineException
which says that the runtime must do JIT in prohibited platform (i.e. iOS). The exception caused by following reasons:
To solve AOT based error, you must use pre-generated serializers deseribed above, or must write manifest for your platform and call following support method.
It is recommended to use pre-generation because it is more testable and stable because the pre-generated serializers are just normal classes from Mono runtime perspective.
However, in many cases, you want to use MessagePack for CLI without mpu
because it takes more time to use serializers.
AOT errors may be resoved via MessagePackSerializer.PrepareType<T>()
, MessagePackSerializer.PrepareCollectionType<TItem>()
, and MessagePackSerializer.PrepareDictionaryType<TKey, TValue>()
respectively. For collection (IEnumerable<T>
types except String
) type, you must call MessagePackSerializer.PrepareCollectionType<TItem>()
with TItem
which is item type of the collection. For dictionary type, you must call MessagePackSerializer.PrepareDictionaryType<TKey, TValue>()
with its key type (TKey
) and value type (TValue
). For other types, you must use MessagePackSerializer.PrepareType<T>()
. Note that when you use own collection type or dictionary type, you need to MessagePackSerializer.PrepareType<T>()
for your collection type in addition to MessagePackSerializer.PrepareCollectionType<TItem>()
or MessagePackSerializer.PrepareDictionaryType<TKey, TValue>()
.
These methods dry run MessagePack for CLI's internal methods to force AOT compilation of the Mono compiler, so they should solve AOT error. If you call these method for non-AOT builds, they do no operations.
They are some known limitations related to AOT runtime. These limitations may be solved in recent implementations or will be solved in future. It is welcome to add or remove entries!
List<int>.Enumerator
) causes ExecutionEngineException
. It is limitations of Mono (for iOS). MessagePack for CLI 0.6 has many workarounds for them, but issues might exist yet.SerializerGenerator
API (or you can do via mpu.exe
command line tool too) to avoid this limitation.MessagePackSerializer.PrepareType<T>()
for value types. It should solve ExecutionEngineException
related to SerializationContext.GetSerializer<T>(object)
or BoxingGenericEqualityComparer
issues of Dictionary<TKey, TValue>
.IEnumerable<int>
) causes ExecutionEngineException
. It is limitations of Unity (for iOS). Consider using boxing. As of 0.6.0, MessagePack for CLI uses boxing and reflections heavily for Unity build to avoid this problem.KeyValuePair<string, DateTimeOffset>
are not supported. To enable them, you must register custom serializer. See test/MsgPack.UnitTest.Unity3D/Serialization/AotWorkarounds.cs for example.System.dll
and System.Core.dll
, so some built-in serializers for FCL collections are disabled.* You can use the build for "full" subset, it is unity3d-full/MsgPack.dll.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4