Skip to content
yfakariya edited this page May 25, 2014 · 8 revisions

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.

Overview

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.

Rationale and History

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.

How to Use in Xamarin

MessagePack for CLI can use in form of a NuGet package.

Refer MessagePack for CLI library

  1. Get NuGet package and install it to Xamarin project. It contains runtime library of MessagePack for CLI. Note that it does not include any code related to runtime code generation.

Generate serializers in advance

  1. Separate serializer generation project from application project. This project is normal desktop project and refers application library project which contains serialization targets. For example, you can use NUnit testing project.
  2. Get NuGet package and install latest MessagePack for CLI package to the project.
  3. Invoke SerializerGenerator API in the project.
  4. Add generated serializers to the project as you like.

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",
                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.

How to Use in Unity

You can import MessagePack for CLI source code to the assets as follows.

Preparation

  1. If you have not been have Unity project, create it.
  2. Remember the root directory of the Assets directory.
  3. Check out MessagePack for CLI source code tree as you like.
  4. Build mpu.exe as follows (of cource, you can build with Visual Studio or MonoDevelop):
%WinDir%\Microsoft.NET\Framework\v3.5\MSBuild.exe .\src\mpu\mpu.csproj /p:Configuration=Release
xbuild .\src\mpu\mpu.csproj -p:Configuration=Release

Import Core Library

  1. Open command prompt or terminal.
  2. Run following command:
powershell .\PackUnity.ps1
.\PackUnity.sh
  1. Import MsgPack.unitypackage

Generate Serializers

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").

  • If you built target types as assembly, you can use following:
mpu.exe -s -a -n MyProduct.MsgPackSerializers -o /path-to-asset-root/MsgPackSerializers /path/to/assembly.dll
  • Or (normal case) you can use following:
mpu.exe -s -n MyProduct.MsgPackSerializers -o /path-to-asset-root/MsgPackSerializers /path/to/sources/Some1.cs /path/to/sources/Some2.cs ...

Limitations of Xamarin

  • Some generics (such as IEnumerable<int>) causes ExecutionEngineException. It is limitations of Mono (for iOS), so please wait for Mono update.
  • Runtime (on-the-fly) generation are not supported even if Android project.
  • You have to pre-generate serializers with SerializerGenerator API (or you can do via mpu.exe command line tool too).

Limitations of Unity

  • Some generics (such as IEnumerable<int>) causes ExecutionEngineException. It is limitations of Unity (for iOS), so please wait for Unity runtime update.
  • Runtime (on-the-fly) generation are not supported even if Android project.
  • You have to pre-generate serializers with SerializerGenerator API (or you can do via mpu.exe command line tool too).