Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protobuf 使用枚举报错 #109

Closed
l-xuanzi opened this issue May 7, 2021 · 18 comments
Closed

protobuf 使用枚举报错 #109

l-xuanzi opened this issue May 7, 2021 · 18 comments

Comments

@l-xuanzi
Copy link

l-xuanzi commented May 7, 2021

测试代码如下

  FieldDescriptorProto descriptorProto = new FieldDescriptorProto();

            descriptorProto.type = FieldDescriptorProto.Type.TYPE_BOOL;
            string ss = "";

            using (var stream = new System.IO.MemoryStream())
            {
                ProtoTypeRegister.Register<FieldDescriptorProto>();
                ProtoBuf.Serializer.Serialize(stream, descriptorProto);
                ss = Convert.ToBase64String(stream.ToArray());
                Debug.Log(ss);
            }
            FieldDescriptorProto reg = ProtoBuf.Serializer.Deserialize(typeof(FieldDescriptorProto),new MemoryStream(Convert.FromBase64String(ss)))  as FieldDescriptorProto;
            //FieldDescriptorProto reg = ProtoBuf.Serializer.Deserialize<FieldDescriptorProto>(new MemoryStream(Convert.FromBase64String(ss)));// 这里有个方法重定向的错误,已经修改

            Debug.Log(reg.type);

报错如下
image

@JasonXuDeveloper
Copy link
Owner

请提供FieldDescriptorProto

@JasonXuDeveloper
Copy link
Owner

没事了,我晚上测试

@l-xuanzi
Copy link
Author

l-xuanzi commented May 7, 2021

@JasonXuDeveloper 就是工程里面test.cs里的

@l-xuanzi
Copy link
Author

l-xuanzi commented May 7, 2021

@JasonXuDeveloper 静待佳音

@JasonXuDeveloper
Copy link
Owner

JasonXuDeveloper commented May 7, 2021

iShot2021-05-07 21 35 00

FieldDescriptorProto这个在Editor下,先不说热更能否正常使用,原生都不行啊,真机下没办法运行这个代码的。`

@JasonXuDeveloper
Copy link
Owner

然后你这个enum必须定义为int类型,例如:

enum Test
{
a,
b,
c
}
enum Test: System.Int32之类的
{
a,
b,
c
}
或者

@l-xuanzi
Copy link
Author

l-xuanzi commented May 7, 2021

@JasonXuDeveloper FieldDescriptorProto消息是工程里面test.proto这个文件用工程提供的toCS工具生成的。。因为发现没法用枚举,所以看这个消息里面有个枚举就直接用这个写了测试代码来进行测试。。

@JasonXuDeveloper
Copy link
Owner

image

这个吗,这个也没有Type.Type_BOOL的定义啊,你定义错了吧

@l-xuanzi
Copy link
Author

l-xuanzi commented May 7, 2021

image

@JasonXuDeveloper
Copy link
Owner

哦哦 我做一个看看

@JasonXuDeveloper
Copy link
Owner

找到问题了,你那个生成的type有个lable属性是nullable,ILRuntime不支持,而且你为null了protobuf无法去序列化

@JasonXuDeveloper
Copy link
Owner

image

image

image

@l-xuanzi
Copy link
Author

l-xuanzi commented May 7, 2021

不是的,,我是ILruntime里用的,报错是说的ILruntime的类型没法装换为枚举,这个Label可以删了的,不影响,有个枚举就行

@JasonXuDeveloper
Copy link
Owner

我知道,我的意思是这个枚举也是null,没办法处理啊

@JasonXuDeveloper
Copy link
Owner

然后我会把enum转换这边修复的

@JasonXuDeveloper
Copy link
Owner

已修复,明早提交到分支

@l-xuanzi
Copy link
Author

l-xuanzi commented May 7, 2021

@JasonXuDeveloper thank you 我明天更新看看

@JasonXuDeveloper
Copy link
Owner

马上提交,然后test.cs的那个类改一下,ilruntime不支持nullable
test.cs的那个类改成:

[global::ProtoBuf.ProtoContract()]
    public partial class FieldDescriptorProto
    {
        [global::ProtoBuf.ProtoMember(1)]
        [global::System.ComponentModel.DefaultValue("")]
        public string name
        {
            get { return __pbn__name ?? ""; }
            set { __pbn__name = value; }
        }
        public bool ShouldSerializename() => __pbn__name != null;
        public void Resetname() => __pbn__name = null;
        private string __pbn__name;

        [global::ProtoBuf.ProtoMember(3)]
        public int number
        {
            get { return __pbn__number; }
            set { __pbn__number = value; }
        }
        public bool ShouldSerializenumber() => __pbn__number != null;
        public void Resetnumber() => __pbn__number = default;
        private int __pbn__number;

        [global::ProtoBuf.ProtoMember(4)]
        [global::System.ComponentModel.DefaultValue(global::google.protobuf.FieldDescriptorProto.Label.LABEL_OPTIONAL)]
        public Label label
        {
            get { return __pbn__label; }
            set { __pbn__label = value; }
        }
        public bool ShouldSerializelabel() => __pbn__label != null;
        public void Resetlabel() => __pbn__label = default;
        private Label __pbn__label;

        [global::ProtoBuf.ProtoMember(5)]
        [global::System.ComponentModel.DefaultValue(global::google.protobuf.FieldDescriptorProto.Type.TYPE_DOUBLE)]
        public Type type
        {
            get { return __pbn__type; }
            set { __pbn__type = value; }
        }
        public bool ShouldSerializetype() => __pbn__type != null;
        public void Resettype() => __pbn__type = default;
        private Type __pbn__type;

        [global::ProtoBuf.ProtoMember(6)]
        [global::System.ComponentModel.DefaultValue("")]
        public string type_name
        {
            get { return __pbn__type_name ?? ""; }
            set { __pbn__type_name = value; }
        }
        public bool ShouldSerializetype_name() => __pbn__type_name != null;
        public void Resettype_name() => __pbn__type_name = null;
        private string __pbn__type_name;

        [global::ProtoBuf.ProtoMember(2)]
        [global::System.ComponentModel.DefaultValue("")]
        public string extendee
        {
            get { return __pbn__extendee ?? ""; }
            set { __pbn__extendee = value; }
        }
        public bool ShouldSerializeextendee() => __pbn__extendee != null;
        public void Resetextendee() => __pbn__extendee = null;
        private string __pbn__extendee;

        [global::ProtoBuf.ProtoMember(7)]
        [global::System.ComponentModel.DefaultValue("")]
        public string default_value
        {
            get { return __pbn__default_value ?? ""; }
            set { __pbn__default_value = value; }
        }
        public bool ShouldSerializedefault_value() => __pbn__default_value != null;
        public void Resetdefault_value() => __pbn__default_value = null;
        private string __pbn__default_value;

        [global::ProtoBuf.ProtoMember(8)]
        public FieldOptions options { get; set; }

        [global::ProtoBuf.ProtoContract()]
        public enum Type
        {
            TYPE_DOUBLE = 1,
            TYPE_FLOAT = 2,
            TYPE_INT64 = 3,
            TYPE_UINT64 = 4,
            TYPE_INT32 = 5,
            TYPE_FIXED64 = 6,
            TYPE_FIXED32 = 7,
            TYPE_BOOL = 8,
            TYPE_STRING = 9,
            TYPE_GROUP = 10,
            TYPE_MESSAGE = 11,
            TYPE_BYTES = 12,
            TYPE_UINT32 = 13,
            TYPE_ENUM = 14,
            TYPE_SFIXED32 = 15,
            TYPE_SFIXED64 = 16,
            TYPE_SINT32 = 17,
            TYPE_SINT64 = 18,
        }

        [global::ProtoBuf.ProtoContract()]
        public enum Label
        {
            LABEL_OPTIONAL = 1,
            LABEL_REQUIRED = 2,
            LABEL_REPEATED = 3,
        }

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants