-
Notifications
You must be signed in to change notification settings - Fork 174
Xamarin and Unity
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.
- 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.
- 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.
- Get NuGet package and install latest MessagePack for CLI package to the project.
- Invoke
SerializerGenerator
API in the project. - 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.
You can import MessagePack for CLI source code to the assets as follows.
- If you have not been have Unity project, create it.
- Remember the root directory of the Assets directory.
- Check out MessagePack for CLI source code tree as you like.
- 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
- Open command prompt or terminal.
- Run following command:
powershell .\PackUnity.ps1
.\PackUnity.sh
- Import
MsgPack.unitypackage
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 ...
- Some generics (such as
IEnumerable<int>
) causesExecutionEngineException
. 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 viampu.exe
command line tool too).
- Some generics (such as
IEnumerable<int>
) causesExecutionEngineException
. 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 viampu.exe
command line tool too).