A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/msgpack/msgpack-cli/wiki/Xamarin-and-Unity below:

Xamarin and Unity · msgpack/msgpack-cli Wiki · GitHub

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 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

Note 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.

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

You must setup serialization context to use generated serializers as following:

  1. (Optional) Create custom SerializationContext and store it as you like.
  2. Register serializer instances with 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.
How to Use in Unity3D using DLL(Recommended)
  1. Download latest zip file from "github release page"(https://github.com/msgpack/msgpack-cli/releases).
  2. Unzip *.zip file and goto bin/Unity3D.Full or bin/Unity3D and find MsgPack.dll file. Note that some tool (including archiver utility in MacOS X) cannot handle Windows style subdirectory correctly, so if you get invalid file which contains back solidous in the name, please try unzipping from terminal (e.g. unzip).
  3. Import the MsgPack.dll to your asset library.
How to Use in Unity3D using Source

If you don't like DLL style asset, you can import MessagePack for CLI source code to the assets as follows.

  1. If you have not been have Unity project, create it.
  2. Remember the root directory of the Assets directory.
  3. Check out source tree.
  1. Open command prompt or terminal on tools/mpu/bin.
  2. Run following command to copy source tree for Unity asset:
mpu.exe -l -o <OUTPUT_DIR>
mono mpu.exe -l -o <OUTPUT_DIR>
  1. Import generated source tree to your Assets library.

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!


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