繁体   English   中英

在Unity-iOS中使用protobuf-net

[英]Using protobuf-net in Unity-iOS

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

我正在按照仓库的自述文件中的说明进行操作。 我正在从此处下载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)显然存在问题。

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

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

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

查看ProtoBuf中是否有一个选项可以禁用动态代码生成。 否则,您可能需要使用其他序列化系统。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM