【问题标题】:Using protobuf-net in Unity-iOS在 Unity-iOS 中使用 protobuf-net
【发布时间】:2018-07-11 20:57:39
【问题描述】:

我是一名 iOS 开发人员,正在开发一个应用程序,该应用程序使用 protobuf 将 Unity 中生成的一些消息作为字节数组传递给 iOS。我使用 protobuf-net (https://github.com/mgravell/protobuf-net) Marc Gravell 在我的 Unity 项目中(我不是 Unity 开发人员)。

我正在按照 repo 的自述文件中的说明进行操作。我正在从这里下载 dll:https://code.google.com/archive/p/protobuf-net/downloads

我这样装饰我的班级:

using ProtoBuf;  

[ProtoContract]
public class AppToPortal
{
    [ProtoMember(1)]
    public uint timestamp_sec { get; set; }
    [ProtoMember(2)]
    public Command command { get; set; }
    [ProtoMember(3)]
    public byte[] accessory_message { get; set; }
}

[ProtoContract]
public class PortalToApp
{
    [ProtoMember(1)]
    public uint timestamp_ms { get; set; }

    [ProtoMember(2)]
    public Event @event { get; set; }

    [ProtoMember(3)]
    public DeviceInfo info { get; set; }

    [ProtoMember(4)]
    public CommandResponse cmdRsp { get; set; }

    [ProtoMember(5)]
    public byte[] accessory_message { get; set; }
}

[ProtoContract]
public class Command
{
    [ProtoMember(1)]
    public CommandType type { get; set; }

    [ProtoMember(2)]
    public byte[] ota_signature { get; set; }

    [ProtoContract]
    public enum CommandType
    {
        ExampleModeOne = 1,
        ExampleModeTwo = 2,
        RequestDeviceInfo = 3,
        TestMode = 4,
        Reset = 5,
        StartOTA = 6,
    }
}  

我的序列化器/反序列化器看起来像

public class Proto
{
    public static byte[] Serialize<T>(T  obj) where T : class
    {
        if (null ==  obj) return null;
        try
        {
            using (var stream = new System.IO.MemoryStream())
            {
                ProtoBuf.Serializer.Serialize<T>(stream,  obj);
                return stream.ToArray();
            }
        }
        catch
        {
            // Log error
            throw;
        }
    }

    public static T Deserialize<T>(byte[] bytes) where T : class
    {
        if (null == bytes) return null;
        try
        {
            using (var stream = new System.IO.MemoryStream(bytes))
            {
                return ProtoBuf.Serializer.Deserialize<T>(stream);
            }
        }
        catch
        {
            // Log error
            throw;
        }
    }
}  

当我撰写邮件时,我正在这样做:

// Compose message with protobuf
TimeSpan timestamp = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
Command command = new Command();
command.type = Command.CommandType.RequestDeviceInfo;

AppToPortal message = new AppToPortal();
message.command = command;
message.timestamp_sec = Convert.ToUInt32(timestamp.TotalSeconds);

Debug.Log(message);

// Serialize message
byte[] bytes = Proto.Serialize<AppToPortal>(message);  

当我反序列化接收到的字节数组时,我正在这样做:

PortalToApp message = Proto.Deserialize<PortalToApp>(bytes);
Debug.Log("Did deserialize: " + message + " - firmware_version: " + message.info.firmware_version + " - battery_level: " + message.info.battery_level);

当我在我的 Mac 上运行它时,我能够序列化/反序列化任何消息,完全没有错误。但是当我将 Unity 项目导出到 iOS 时,我在 Xcode 控制台中收到以下错误消息:

(Filename: /Users/builduser/buildslave/unity/build/Runtime/Export/Debug.bindings.h Line: 43)

NotSupportedException: /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/DynamicMethod.cpp(19) :  Unsupported internal call for IL2CPP:DynamicMethod::create_dynamic_method - System.Reflection.Emit is not supported.
    at System.Reflection.Emit.DynamicMethod.CreateDynMethod () [0x00000] in <filename unknown>:0 
    at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Compiler.CompilerContext.BuildSerializer (IProtoSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer..ctor (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer.Wrap (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.MetaType.get_Serializer () [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (Int32 key, System.Object value, ProtoBuf.ProtoReader source) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializer.Deserialize[T] (System.IO.Stream source) [0x00000] in <filename unknown>:0 
    at MCPP.Proto.Deserialize[T] (System.Byte[] bytes) [0x00000] in <filename unknown>:0 
    at MenuPanel.OnReceiveData (MCPP.Notification notification) [0x00000] in <filename unknown>:0 
    at MCPP.MCPPKit.PostNotification (MCPP.Notification aNotification) [0x00000] in <filename unknown>:0 
    at MCPP.IOSPlugin.didReceiveDataCallback (IntPtr ptr, Int32 length) [0x00000] in <filename unknown>:0 
Rethrow as InvalidOperationException: It was not possible to prepare a serializer for: MCPP.PortalToApp
    at ProtoBuf.Compiler.CompilerContext.BuildSerializer (IProtoSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer..ctor (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer.Wrap (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.MetaType.get_Serializer () [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (Int32 key, System.Object value, ProtoBuf.ProtoReader source) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializer.Deserialize[T] (System.IO.Stream source) [0x00000] in <filename unknown>:0 
    at MCPP.Proto.Deserialize[T] (System.Byte[] bytes) [0x00000] in <filename unknown>:0 
    at MenuPanel.OnReceiveData (MCPP.Notification notification) [0x00000] in <filename unknown>:0 
    at MCPP.MCPPKit.PostNotification (MCPP.Notification aNotification) [0x00000] in <filename unknown>:0 
    at MCPP.IOSPlugin.didReceiveDataCallback (IntPtr ptr, Int32 length) [0x00000] in <filename unknown>:0 
MCPP.MCPPKit:PostNotification(Notification)
MCPP.IOSPlugin:didReceiveDataCallback(IntPtr, Int32)

(Filename: currently not available on il2cpp Line: -1)

NotSupportedException: /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/DynamicMethod.cpp(24) :  Unsupported internal call for IL2CPP:DynamicMethod::destroy_dynamic_method - System.Reflection.Emit is not supported.
    at System.Reflection.Emit.DynamicMethod.Finalize () [0x00000] in <filename unknown>:0 
UnityEngine.UnhandledExceptionHandler:PrintException(String, Exception)
UnityEngine.UnhandledExceptionHandler:HandleUnhandledException(Object, UnhandledExceptionEventArgs)

(Filename: currently not available on il2cpp Line: -1)

ProtoBuf.Serializer.Deserialize(stream) 和 ProtoBuf.Serializer.Serialize(stream, obj) 显然有问题。

你知道我做错了什么吗?任何帮助将不胜感激。

【问题讨论】:

    标签: c# ios xcode unity3d protobuf-net


    【解决方案1】:

    您不能在 iOS 上使用 Reflection.Emit,因为 iOS 不允许 JIT 编译。

    这就是 NotSupportedException 来自 ...Reflection.Emit.DynamicMethod.CreateDynMethod 的原因,ProtoBuf 使用它在运行时生成高效的序列化代码。

    看看 ProtoBuf 中是否有禁用动态代码生成的选项。否则,您可能需要使用不同的序列化系统。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多