4

First a big thanks to Marc Gravell author of protobuf-net http://code.google.com/p/protobuf-net/ . It's a really great resource. Anyhow I am hoping that Marc or someone else can help me resolve this exception.

I am trying to implement protobuf-net on mobile devices (iOS and Android) using Unity3D game engine. Unity 3.2 uses Mono 2.6, their slightly modified version of mono.

It works fine in the Editor, but on the iOS device at runtime it fails at the first member it tries to serialize. Note the --aot-only flag in the exception. I think Unity basically builds ARM assembly through Mono's aot feature though I don't understand what it's doing under the hood.

ExecutionEngineException: Attempting to JIT compile method 'ProtoBuf.Property.Property`2<GameManagerSaveState , bool>:.ctor ()' while running with --aot-only.

at ProtoBuf.Property.PropertyBoolean`1[GameManagerSaveState]..ctor () [0x00000] in <filename unknown>:0 
at ProtoBuf.Property.PropertyFactory.CreateProperty[GameManagerSaveState] (System.Type type, ProtoBuf.DataFormat& format, MemberSerializationOptions options) [0x00000] in <filename unknown>:0 
at ProtoBuf.Property.PropertyFactory.Create[GameManagerSaveState] (System.Reflection.MemberInfo member) [0x00000] in <filename unknown>:0 
at ProtoBuf.Serializer`1[GameManagerSaveState].Build () [0x00000] in <filename unknown>:0

It was suggested on IRC that I could declare variables of these types ahead of time, that the compiler would not have to JIT them at runtime. Seems like a great idea, but unfortunately these are like internal generic types, and don't know what to write in C# to declare variables in order to fake out the compiler. Can anyone interpret the above message and let me know what the compiler needs to know in advance? Any other strategies for preventing this from happening? BTW Here is the top of the class where it's erroring

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf;

[ProtoContract]
public class GameManagerSaveState
{
    [ProtoMember(1)]
    public bool gameOver;
      // snip

Thanks Mono and protobuf experts for helping me nail this one! protobuf-net seems to be an awesome and lightweight serialization and RPC wire protocol that would be perfect for iOS and Android apps, so I am looking forward to using it.

2 Answers 2

4

There are a number of issues in v1 that make it a bit of a pain to pre-JIT; v2, although incomplete, is intended to resolve many/all of these issues, not least by offering a pre-compile to dll option - but even the runtime-only version should be much more device friendly.

I should also mention that Jon's port of the Java version should work well here, since it is compiler-heavy rather than runtime-heavy.

6
  • 1
    Mark, OK thanks much. I am using r282. Is that v1? I guess v2 is not released yet, going from your notes on the website. I'll look into the Java ported version, unless you can point me towards v2. :)
    – Alex Rice
    Feb 23, 2011 at 23:42
  • 1
    @Alex - v2 is available as code only from the trunk; r282 is v1 Feb 24, 2011 at 6:55
  • @Mark ok thanks. Btw I did some testing with Skeet's protobuf-csharp-port. Unfortunately I get ExecutionEngineException: Attempting to JIT compile method 'System.Collections.Generic.GenericEqualityComparer`1<Google.ProtocolBuffers.ExtensionRegistry/DescriptorIntPair>:.ctor ()' while running with --aot-only.
    – Alex Rice
    Feb 24, 2011 at 13:38
  • So I am going to try your v2 from trunk. Understand it's early days and it may not work though. Thanks again.
    – Alex Rice
    Feb 24, 2011 at 13:39
  • 1
    @Alex for the record, ive ha it working in the emu. Let me know if you get stuck, but I'm a couple of MonoDroid betas behind - easy enough to update, of course. Feb 24, 2011 at 17:37
1

Use this before using protobuf:

Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");

Credit: https://github.com/antonholmquist/easy-serializer-unity

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.