static int ParseDelimitedFrom(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1)
            {
                System.IO.Stream  arg0 = (System.IO.Stream)ToLua.CheckObject <System.IO.Stream>(L, 1);
                MyLib.CGPlayerCmd o    = MyLib.CGPlayerCmd.ParseDelimitedFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2)
            {
                System.IO.Stream arg0 = (System.IO.Stream)ToLua.CheckObject <System.IO.Stream>(L, 1);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.CheckObject(L, 2, typeof(Google.ProtocolBuffers.ExtensionRegistry));
                MyLib.CGPlayerCmd o = MyLib.CGPlayerCmd.ParseDelimitedFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: MyLib.CGPlayerCmd.ParseDelimitedFrom"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
    static int MergeDelimitedFrom(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 2)
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                System.IO.Stream          arg0 = (System.IO.Stream)ToLua.CheckObject <System.IO.Stream>(L, 2);
                MyLib.GCPlayerCmd.Builder o    = obj.MergeDelimitedFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 3)
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                System.IO.Stream arg0 = (System.IO.Stream)ToLua.CheckObject <System.IO.Stream>(L, 2);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.CheckObject(L, 3, typeof(Google.ProtocolBuffers.ExtensionRegistry));
                MyLib.GCPlayerCmd.Builder o = obj.MergeDelimitedFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: Google.ProtocolBuffers.AbstractBuilderLite<MyLib.GCPlayerCmd,MyLib.GCPlayerCmd.Builder>.MergeDelimitedFrom"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
        protected override bool ParseUnknownField(ICodedInputStream input, ExtensionRegistry extensionRegistry, uint tag, string fieldName)
        {
            TMessage messageBeingBuilt = this.MessageBeingBuilt;
            FieldSet extensions        = messageBeingBuilt.Extensions;

            WireFormat.WireType tagWireType = WireFormat.GetTagWireType(tag);
            int tagFieldNumber = WireFormat.GetTagFieldNumber(tag);
            IGeneratedExtensionLite generatedExtensionLite = extensionRegistry[this.DefaultInstanceForType, tagFieldNumber];

            if (generatedExtensionLite == null)
            {
                return(input.SkipField());
            }
            IFieldDescriptorLite descriptor = generatedExtensionLite.Descriptor;

            if (descriptor == null)
            {
                return(input.SkipField());
            }
            WireFormat.WireType wireType = descriptor.IsPacked ? WireFormat.WireType.LengthDelimited : WireFormat.GetWireType(descriptor.FieldType);
            if (tagWireType != wireType)
            {
                wireType = WireFormat.GetWireType(descriptor.FieldType);
                if (tagWireType != wireType && (!descriptor.IsRepeated || tagWireType != WireFormat.WireType.LengthDelimited || (wireType != WireFormat.WireType.Varint && wireType != WireFormat.WireType.Fixed32 && wireType != WireFormat.WireType.Fixed64)))
                {
                    return(input.SkipField());
                }
            }
            if (!descriptor.IsRepeated && tagWireType != WireFormat.GetWireType(descriptor.FieldType))
            {
                return(input.SkipField());
            }
            FieldType fieldType = descriptor.FieldType;

            switch (fieldType)
            {
            case FieldType.Group:
            case FieldType.Message:
            {
                if (descriptor.IsRepeated)
                {
                    List <IMessageLite> list = new List <IMessageLite>();
                    if (descriptor.FieldType == FieldType.Group)
                    {
                        input.ReadGroupArray <IMessageLite>(tag, fieldName, list, generatedExtensionLite.MessageDefaultInstance, extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessageArray <IMessageLite>(tag, fieldName, list, generatedExtensionLite.MessageDefaultInstance, extensionRegistry);
                    }
                    using (List <IMessageLite> .Enumerator enumerator = list.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            IMessageLite current = enumerator.Current;
                            extensions.AddRepeatedField(descriptor, current);
                        }
                    }
                    return(true);
                }
                IMessageLite messageLite = extensions[generatedExtensionLite.Descriptor] as IMessageLite;
                IBuilderLite builderLite = (messageLite ?? generatedExtensionLite.MessageDefaultInstance).WeakToBuilder();
                if (descriptor.FieldType == FieldType.Group)
                {
                    input.ReadGroup(descriptor.FieldNumber, builderLite, extensionRegistry);
                }
                else
                {
                    input.ReadMessage(builderLite, extensionRegistry);
                }
                extensions[descriptor] = builderLite.WeakBuild();
                break;
            }

            default:
                if (fieldType == FieldType.Enum)
                {
                    if (!descriptor.IsRepeated)
                    {
                        IEnumLite value = null;
                        object    obj;
                        if (input.ReadEnum(ref value, out obj, descriptor.EnumType))
                        {
                            extensions[descriptor] = value;
                            break;
                        }
                        break;
                    }
                    else
                    {
                        List <IEnumLite>     list2 = new List <IEnumLite>();
                        ICollection <object> collection;
                        input.ReadEnumArray(tag, fieldName, list2, out collection, descriptor.EnumType);
                        using (List <IEnumLite> .Enumerator enumerator2 = list2.GetEnumerator())
                        {
                            while (enumerator2.MoveNext())
                            {
                                IEnumLite current2 = enumerator2.Current;
                                extensions.AddRepeatedField(descriptor, current2);
                            }
                            break;
                        }
                    }
                }
                if (!descriptor.IsRepeated)
                {
                    object value2 = null;
                    if (input.ReadPrimitiveField(descriptor.FieldType, ref value2))
                    {
                        extensions[descriptor] = value2;
                    }
                }
                else
                {
                    List <object> list3 = new List <object>();
                    input.ReadPrimitiveArray(descriptor.FieldType, tag, fieldName, list3);
                    using (List <object> .Enumerator enumerator3 = list3.GetEnumerator())
                    {
                        while (enumerator3.MoveNext())
                        {
                            object current3 = enumerator3.Current;
                            extensions.AddRepeatedField(descriptor, current3);
                        }
                    }
                }
                break;
            }
            return(true);
        }
            /// <summary>
            /// Like <see cref="MergeFrom(ICodedInputStream, ExtensionRegistry, IBuilder)" />
            /// but parses a single field.
            /// </summary>
            /// <param name="input">The input to read the field from</param>
            /// <param name="extensionRegistry">Registry to use when an extension field is encountered</param>
            /// <param name="builder">Builder to merge field into, if it's a known field</param>
            /// <param name="tag">The tag, which should already have been read from the input</param>
            /// <returns>true unless the tag is an end-group tag</returns>
            internal bool MergeFieldFrom(ICodedInputStream input,
                                         ExtensionRegistry extensionRegistry, IBuilder builder, uint tag,
                                         string fieldName)
            {
                if (tag == 0 && fieldName != null)
                {
                    FieldDescriptor fieldByName = builder.DescriptorForType.FindFieldByName(fieldName);
                    if (fieldByName != null)
                    {
                        tag = WireFormat.MakeTag(fieldByName);
                    }
                    else
                    {
                        ExtensionInfo extension = extensionRegistry.FindByName(builder.DescriptorForType, fieldName);
                        if (extension != null)
                        {
                            tag = WireFormat.MakeTag(extension.Descriptor);
                        }
                    }
                }

                MessageDescriptor type = builder.DescriptorForType;

                if (type.Options.MessageSetWireFormat && tag == WireFormat.MessageSetTag.ItemStart)
                {
                    MergeMessageSetExtensionFromCodedStream(input, extensionRegistry, builder);
                    return(true);
                }

                WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
                int fieldNumber = WireFormat.GetTagFieldNumber(tag);

                FieldDescriptor field;
                IMessageLite    defaultFieldInstance = null;

                if (type.IsExtensionNumber(fieldNumber))
                {
                    ExtensionInfo extension = extensionRegistry[type, fieldNumber];
                    if (extension == null)
                    {
                        field = null;
                    }
                    else
                    {
                        field = extension.Descriptor;
                        defaultFieldInstance = extension.DefaultInstance;
                    }
                }
                else
                {
                    field = type.FindFieldByNumber(fieldNumber);
                }

                // Unknown field or wrong wire type. Skip.
                if (field == null)
                {
                    return(MergeFieldFrom(tag, input));
                }
                if (wireType != WireFormat.GetWireType(field))
                {
                    WireFormat.WireType expectedType = WireFormat.GetWireType(field.FieldType);
                    if (wireType == expectedType)
                    {
                        //Allowed as of 2.3, this is unpacked data for a packed array
                    }
                    else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited &&
                             (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 ||
                              expectedType == WireFormat.WireType.Fixed64))
                    {
                        //Allowed as of 2.3, this is packed data for an unpacked array
                    }
                    else
                    {
                        return(MergeFieldFrom(tag, input));
                    }
                }

                switch (field.FieldType)
                {
                case FieldType.Group:
                case FieldType.Message:
                {
                    IBuilderLite subBuilder = (defaultFieldInstance != null)
                                                          ? defaultFieldInstance.WeakCreateBuilderForType()
                                                          : builder.CreateBuilderForField(field);
                    if (!field.IsRepeated)
                    {
                        subBuilder.WeakMergeFrom((IMessageLite)builder[field]);
                        if (field.FieldType == FieldType.Group)
                        {
                            input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
                        }
                        else
                        {
                            input.ReadMessage(subBuilder, extensionRegistry);
                        }
                        builder[field] = subBuilder.WeakBuild();
                    }
                    else
                    {
                        List <IMessageLite> list = new List <IMessageLite>();
                        if (field.FieldType == FieldType.Group)
                        {
                            input.ReadGroupArray(tag, fieldName, list, subBuilder.WeakDefaultInstanceForType,
                                                 extensionRegistry);
                        }
                        else
                        {
                            input.ReadMessageArray(tag, fieldName, list, subBuilder.WeakDefaultInstanceForType,
                                                   extensionRegistry);
                        }

                        foreach (IMessageLite m in list)
                        {
                            builder.WeakAddRepeatedField(field, m);
                        }
                        return(true);
                    }
                    break;
                }

                case FieldType.Enum:
                {
                    if (!field.IsRepeated)
                    {
                        object    unknown;
                        IEnumLite value = null;
                        if (input.ReadEnum(ref value, out unknown, field.EnumType))
                        {
                            builder[field] = value;
                        }
                        else if (unknown is int)
                        {
                            MergeVarintField(fieldNumber, (ulong)(int)unknown);
                        }
                    }
                    else
                    {
                        ICollection <object> unknown;
                        List <IEnumLite>     list = new List <IEnumLite>();
                        input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType);

                        foreach (IEnumLite en in list)
                        {
                            builder.WeakAddRepeatedField(field, en);
                        }

                        if (unknown != null)
                        {
                            foreach (object oval in unknown)
                            {
                                if (oval is int)
                                {
                                    MergeVarintField(fieldNumber, (ulong)(int)oval);
                                }
                            }
                        }
                    }
                    break;
                }

                default:
                {
                    if (!field.IsRepeated)
                    {
                        object value = null;
                        if (input.ReadPrimitiveField(field.FieldType, ref value))
                        {
                            builder[field] = value;
                        }
                    }
                    else
                    {
                        List <object> list = new List <object>();
                        input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list);
                        foreach (object oval in list)
                        {
                            builder.WeakAddRepeatedField(field, oval);
                        }
                    }
                    break;
                }
                }
                return(true);
            }
Beispiel #5
0
        /// <summary>
        /// Called by subclasses to parse an unknown field or an extension.
        /// </summary>
        /// <returns>true unless the tag is an end-group tag</returns>
        protected override bool ParseUnknownField(ICodedInputStream input,
                                                  ExtensionRegistry extensionRegistry, uint tag, string fieldName)
        {
            FieldSet extensions = MessageBeingBuilt.Extensions;

            WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
            int fieldNumber = WireFormat.GetTagFieldNumber(tag);
            IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber];

            if (extension == null) //unknown field
            {
                return(input.SkipField());
            }

            IFieldDescriptorLite field = extension.Descriptor;


            // Unknown field or wrong wire type. Skip.
            if (field == null)
            {
                return(input.SkipField());
            }
            WireFormat.WireType expectedType = field.IsPacked
                                                   ? WireFormat.WireType.LengthDelimited
                                                   : WireFormat.GetWireType(field.FieldType);
            if (wireType != expectedType)
            {
                expectedType = WireFormat.GetWireType(field.FieldType);
                if (wireType == expectedType)
                {
                    //Allowed as of 2.3, this is unpacked data for a packed array
                }
                else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited &&
                         (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 ||
                          expectedType == WireFormat.WireType.Fixed64))
                {
                    //Allowed as of 2.3, this is packed data for an unpacked array
                }
                else
                {
                    return(input.SkipField());
                }
            }
            if (!field.IsRepeated && wireType != WireFormat.GetWireType(field.FieldType)) //invalid wire type
            {
                return(input.SkipField());
            }

            switch (field.FieldType)
            {
            case FieldType.Group:
            case FieldType.Message:
            {
                if (!field.IsRepeated)
                {
                    IMessageLite message    = extensions[extension.Descriptor] as IMessageLite;
                    IBuilderLite subBuilder = (message ?? extension.MessageDefaultInstance).WeakToBuilder();

                    if (field.FieldType == FieldType.Group)
                    {
                        input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessage(subBuilder, extensionRegistry);
                    }

                    extensions[field] = subBuilder.WeakBuild();
                }
                else
                {
                    List <IMessageLite> list = new List <IMessageLite>();
                    if (field.FieldType == FieldType.Group)
                    {
                        input.ReadGroupArray(tag, fieldName, list, extension.MessageDefaultInstance,
                                             extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessageArray(tag, fieldName, list, extension.MessageDefaultInstance,
                                               extensionRegistry);
                    }

                    foreach (IMessageLite m in list)
                    {
                        extensions.AddRepeatedField(field, m);
                    }
                    return(true);
                }
                break;
            }

            case FieldType.Enum:
            {
                if (!field.IsRepeated)
                {
                    object    unknown;
                    IEnumLite value = null;
                    if (input.ReadEnum(ref value, out unknown, field.EnumType))
                    {
                        extensions[field] = value;
                    }
                }
                else
                {
                    ICollection <object> unknown;
                    List <IEnumLite>     list = new List <IEnumLite>();
                    input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType);

                    foreach (IEnumLite en in list)
                    {
                        extensions.AddRepeatedField(field, en);
                    }
                }
                break;
            }

            default:
            {
                if (!field.IsRepeated)
                {
                    object value = null;
                    if (input.ReadPrimitiveField(field.FieldType, ref value))
                    {
                        extensions[field] = value;
                    }
                }
                else
                {
                    List <object> list = new List <object>();
                    input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list);
                    foreach (object oval in list)
                    {
                        extensions.AddRepeatedField(field, oval);
                    }
                }
                break;
            }
            }

            return(true);
        }
 public override Builder MergeFrom(ICodedInputStream input, ExtensionRegistry extensionRegistry)
 {
     wrappedBuilder.WeakMergeFrom(input, extensionRegistry);
     return(this);
 }
Beispiel #7
0
 IBuilderLite IBuilderLite.WeakMergeFrom(CodedInputStream input, ExtensionRegistry registry)
 {
     return(MergeFrom(input));
 }
Beispiel #8
0
            /// <summary>
            /// Called by MergeFieldFrom to parse a MessageSet extension.
            /// </summary>
            private void MergeMessageSetExtensionFromCodedStream(CodedInputStream input,
                                                                 ExtensionRegistry extensionRegistry, IBuilder builder)
            {
                MessageDescriptor type = builder.DescriptorForType;

                // The wire format for MessageSet is:
                //   message MessageSet {
                //     repeated group Item = 1 {
                //       required int32 typeId = 2;
                //       required bytes message = 3;
                //     }
                //   }
                // "typeId" is the extension's field number.  The extension can only be
                // a message type, where "message" contains the encoded bytes of that
                // message.
                //
                // In practice, we will probably never see a MessageSet item in which
                // the message appears before the type ID, or where either field does not
                // appear exactly once.  However, in theory such cases are valid, so we
                // should be prepared to accept them.

                int             typeId     = 0;
                ByteString      rawBytes   = null; // If we encounter "message" before "typeId"
                IBuilderLite    subBuilder = null;
                FieldDescriptor field      = null;

                while (true)
                {
                    uint tag = input.ReadTag();
                    if (tag == 0)
                    {
                        break;
                    }

                    if (tag == WireFormat.MessageSetTag.TypeID)
                    {
                        typeId = input.ReadInt32();
                        // Zero is not a valid type ID.
                        if (typeId != 0)
                        {
                            ExtensionInfo extension = extensionRegistry[type, typeId];
                            if (extension != null)
                            {
                                field      = extension.Descriptor;
                                subBuilder = extension.DefaultInstance.WeakCreateBuilderForType();
                                IMessageLite originalMessage = (IMessageLite)builder[field];
                                if (originalMessage != null)
                                {
                                    subBuilder.WeakMergeFrom(originalMessage);
                                }
                                if (rawBytes != null)
                                {
                                    // We already encountered the message.  Parse it now.
                                    // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes.
                                    // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry?
                                    subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput());
                                    rawBytes = null;
                                }
                            }
                            else
                            {
                                // Unknown extension number.  If we already saw data, put it
                                // in rawBytes.
                                if (rawBytes != null)
                                {
                                    MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build());
                                    rawBytes = null;
                                }
                            }
                        }
                    }
                    else if (tag == WireFormat.MessageSetTag.Message)
                    {
                        if (typeId == 0)
                        {
                            // We haven't seen a type ID yet, so we have to store the raw bytes for now.
                            rawBytes = input.ReadBytes();
                        }
                        else if (subBuilder == null)
                        {
                            // We don't know how to parse this.  Ignore it.
                            MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(input.ReadBytes()).Build());
                        }
                        else
                        {
                            // We already know the type, so we can parse directly from the input
                            // with no copying.  Hooray!
                            input.ReadMessage(subBuilder, extensionRegistry);
                        }
                    }
                    else
                    {
                        // Unknown tag.  Skip it.
                        if (!input.SkipField(tag))
                        {
                            break; // end of group
                        }
                    }
                }

                input.CheckLastTagWas(WireFormat.MessageSetTag.ItemEnd);

                if (subBuilder != null)
                {
                    builder[field] = subBuilder.WeakBuild();
                }
            }
Beispiel #9
0
 public static void Merge(TextReader reader, ExtensionRegistry registry, IBuilder builder)
 {
     Merge(reader.ReadToEnd(), registry, builder);
 }
        protected override bool ParseUnknownField(CodedInputStream input,
                                                  ExtensionRegistry extensionRegistry, uint tag)
        {
            FieldSet extensions = MessageBeingBuilt.Extensions;

            WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
            int fieldNumber = WireFormat.GetTagFieldNumber(tag);
            IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber];

            bool unknown = false;
            bool packed  = false;

            if (extension == null)
            {
                unknown = true; // Unknown field.
            }
            else if (wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, false /* isPacked */))
            {
                packed = false; // Normal, unpacked value.
            }
            else if (extension.Descriptor.IsRepeated &&
                     //?? just returns true ?? extension.Descriptor.type.isPackable() &&
                     wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, true /* isPacked */))
            {
                packed = true; // Packed value.
            }
            else
            {
                unknown = true; // Wrong wire type.
            }

            if (unknown) // Unknown field or wrong wire type.  Skip.
            {
                return(input.SkipField(tag));
            }

            if (packed)
            {
                int length = (int)Math.Min(int.MaxValue, input.ReadRawVarint32());
                int limit  = input.PushLimit(length);
                if (extension.Descriptor.FieldType == FieldType.Enum)
                {
                    while (!input.ReachedLimit)
                    {
                        int    rawValue = input.ReadEnum();
                        Object value    =
                            extension.Descriptor.EnumType.FindValueByNumber(rawValue);
                        if (value == null)
                        {
                            // If the number isn't recognized as a valid value for this
                            // enum, drop it (don't even add it to unknownFields).
                            return(true);
                        }
                        extensions.AddRepeatedField(extension.Descriptor, value);
                    }
                }
                else
                {
                    while (!input.ReachedLimit)
                    {
                        Object value = input.ReadPrimitiveField(extension.Descriptor.FieldType);
                        extensions.AddRepeatedField(extension.Descriptor, value);
                    }
                }
                input.PopLimit(limit);
            }
            else
            {
                Object value;
                switch (extension.Descriptor.MappedType)
                {
                case MappedType.Message: {
                    IBuilderLite subBuilder = null;
                    if (!extension.Descriptor.IsRepeated)
                    {
                        IMessageLite existingValue = extensions[extension.Descriptor] as IMessageLite;
                        if (existingValue != null)
                        {
                            subBuilder = existingValue.WeakToBuilder();
                        }
                    }
                    if (subBuilder == null)
                    {
                        subBuilder = extension.MessageDefaultInstance.WeakCreateBuilderForType();
                    }
                    if (extension.Descriptor.FieldType == FieldType.Group)
                    {
                        input.ReadGroup(extension.Number, subBuilder, extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessage(subBuilder, extensionRegistry);
                    }
                    value = subBuilder.WeakBuild();
                    break;
                }

                case MappedType.Enum:
                    int rawValue = input.ReadEnum();
                    value = extension.Descriptor.EnumType.FindValueByNumber(rawValue);
                    // If the number isn't recognized as a valid value for this enum,
                    // drop it.
                    if (value == null)
                    {
                        return(true);
                    }
                    break;

                default:
                    value = input.ReadPrimitiveField(extension.Descriptor.FieldType);
                    break;
                }

                if (extension.Descriptor.IsRepeated)
                {
                    extensions.AddRepeatedField(extension.Descriptor, value);
                }
                else
                {
                    extensions[extension.Descriptor] = value;
                }
            }

            return(true);
        }
Beispiel #11
0
        public void ExtensionWriterTestPacked()
        {
            TestPackedExtensionsLite.Builder builder = TestPackedExtensionsLite.CreateBuilder()
                                                       .AddExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, true)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, true)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 123u);

            TestPackedExtensionsLite msg = builder.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            TestPackedExtensionsLite.Builder copyBuilder = TestPackedExtensionsLite.CreateBuilder().MergeFrom(msg.ToByteArray(), registry);
            TestPackedExtensionsLite         copy        = copyBuilder.Build();

            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());

            Assert.AreEqual(true, copy.GetExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 0));

            Assert.AreEqual(true, copy.GetExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 1));
        }
    static int ParseFrom(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1 && TypeChecker.CheckTypes <Google.ProtocolBuffers.ByteString>(L, 1))
            {
                Google.ProtocolBuffers.ByteString arg0 = (Google.ProtocolBuffers.ByteString)ToLua.ToObject(L, 1);
                MyLib.CGPlayerCmd o = MyLib.CGPlayerCmd.ParseFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 1 && TypeChecker.CheckTypes <byte[]>(L, 1))
            {
                byte[]            arg0 = ToLua.CheckByteBuffer(L, 1);
                MyLib.CGPlayerCmd o    = MyLib.CGPlayerCmd.ParseFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 1 && TypeChecker.CheckTypes <System.IO.Stream>(L, 1))
            {
                System.IO.Stream  arg0 = (System.IO.Stream)ToLua.ToObject(L, 1);
                MyLib.CGPlayerCmd o    = MyLib.CGPlayerCmd.ParseFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 1 && TypeChecker.CheckTypes <Google.ProtocolBuffers.CodedInputStream>(L, 1))
            {
                Google.ProtocolBuffers.CodedInputStream arg0 = (Google.ProtocolBuffers.CodedInputStream)ToLua.ToObject(L, 1);
                MyLib.CGPlayerCmd o = MyLib.CGPlayerCmd.ParseFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <Google.ProtocolBuffers.ByteString, Google.ProtocolBuffers.ExtensionRegistry>(L, 1))
            {
                Google.ProtocolBuffers.ByteString        arg0 = (Google.ProtocolBuffers.ByteString)ToLua.ToObject(L, 1);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 2);
                MyLib.CGPlayerCmd o = MyLib.CGPlayerCmd.ParseFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <byte[], Google.ProtocolBuffers.ExtensionRegistry>(L, 1))
            {
                byte[] arg0 = ToLua.CheckByteBuffer(L, 1);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 2);
                MyLib.CGPlayerCmd o = MyLib.CGPlayerCmd.ParseFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <System.IO.Stream, Google.ProtocolBuffers.ExtensionRegistry>(L, 1))
            {
                System.IO.Stream arg0 = (System.IO.Stream)ToLua.ToObject(L, 1);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 2);
                MyLib.CGPlayerCmd o = MyLib.CGPlayerCmd.ParseFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <Google.ProtocolBuffers.CodedInputStream, Google.ProtocolBuffers.ExtensionRegistry>(L, 1))
            {
                Google.ProtocolBuffers.CodedInputStream  arg0 = (Google.ProtocolBuffers.CodedInputStream)ToLua.ToObject(L, 1);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 2);
                MyLib.CGPlayerCmd o = MyLib.CGPlayerCmd.ParseFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: MyLib.CGPlayerCmd.ParseFrom"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Beispiel #13
0
        /// <summary>
        /// Parse a message of the given type from the given stream and extension registry.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="input"></param>
        /// <param name="extensionRegistry"></param>
        /// <returns></returns>
        public static DynamicMessage ParseFrom(MessageDescriptor type, CodedInputStream input, ExtensionRegistry extensionRegistry)
        {
            Builder builder        = CreateBuilder(type);
            Builder dynamicBuilder = builder.MergeFrom(input, extensionRegistry);

            return(dynamicBuilder.BuildParsed());
        }
Beispiel #14
0
        /// <summary>
        /// Parse <paramref name="data"/> as a message of the given type and return it.
        /// </summary>
        public static DynamicMessage ParseFrom(MessageDescriptor type, ByteString data, ExtensionRegistry extensionRegistry)
        {
            Builder builder        = CreateBuilder(type);
            Builder dynamicBuilder = builder.MergeFrom(data, extensionRegistry);

            return(dynamicBuilder.BuildParsed());
        }
Beispiel #15
0
 /// <summary>
 /// Called by subclasses to parse an unknown field or an extension.
 /// </summary>
 /// <returns>true unless the tag is an end-group tag</returns>
 protected override bool ParseUnknownField(ICodedInputStream input, UnknownFieldSet.Builder unknownFields,
                                           ExtensionRegistry extensionRegistry, uint tag, string fieldName)
 {
     return(unknownFields.MergeFieldFrom(input, extensionRegistry, this, tag, fieldName));
 }
Beispiel #16
0
        /// <summary>
        /// Parses a single field from the specified tokenizer and merges it into
        /// the builder.
        /// </summary>
        private static void MergeField(TextTokenizer tokenizer, ExtensionRegistry extensionRegistry,
                                       IBuilder builder)
        {
            FieldDescriptor   field;
            MessageDescriptor type      = builder.DescriptorForType;
            ExtensionInfo     extension = null;

            if (tokenizer.TryConsume("["))
            {
                // An extension.
                StringBuilder name = new StringBuilder(tokenizer.ConsumeIdentifier());
                while (tokenizer.TryConsume("."))
                {
                    name.Append(".");
                    name.Append(tokenizer.ConsumeIdentifier());
                }

                extension = extensionRegistry.FindByName(type, name.ToString());

                if (extension == null)
                {
                    throw tokenizer.CreateFormatExceptionPreviousToken("Extension \"" + name +
                                                                       "\" not found in the ExtensionRegistry.");
                }
                else if (extension.Descriptor.ContainingType != type)
                {
                    throw tokenizer.CreateFormatExceptionPreviousToken("Extension \"" + name +
                                                                       "\" does not extend message type \"" +
                                                                       type.FullName + "\".");
                }

                tokenizer.Consume("]");

                field = extension.Descriptor;
            }
            else
            {
                String name = tokenizer.ConsumeIdentifier();
                field = type.FindDescriptor <FieldDescriptor>(name);

                // Group names are expected to be capitalized as they appear in the
                // .proto file, which actually matches their type names, not their field
                // names.
                if (field == null)
                {
                    // Explicitly specify the invariant culture so that this code does not break when
                    // executing in Turkey.
                    String lowerName = name.ToLowerInvariant();
                    field = type.FindDescriptor <FieldDescriptor>(lowerName);
                    // If the case-insensitive match worked but the field is NOT a group,
                    // TODO(jonskeet): What? Java comment ends here!
                    if (field != null && field.FieldType != FieldType.Group)
                    {
                        field = null;
                    }
                }
                // Again, special-case group names as described above.
                if (field != null && field.FieldType == FieldType.Group && field.MessageType.Name != name)
                {
                    field = null;
                }

                if (field == null)
                {
                    throw tokenizer.CreateFormatExceptionPreviousToken(
                              "Message type \"" + type.FullName + "\" has no field named \"" + name + "\".");
                }
            }

            object value = null;

            if (field.MappedType == MappedType.Message)
            {
                tokenizer.TryConsume(":"); // optional

                String endToken;
                if (tokenizer.TryConsume("<"))
                {
                    endToken = ">";
                }
                else
                {
                    tokenizer.Consume("{");
                    endToken = "}";
                }

                IBuilder subBuilder;
                if (extension == null)
                {
                    subBuilder = builder.CreateBuilderForField(field);
                }
                else
                {
                    subBuilder = extension.DefaultInstance.WeakCreateBuilderForType() as IBuilder;
                    if (subBuilder == null)
                    {
                        throw new NotSupportedException("Lite messages are not supported.");
                    }
                }

                while (!tokenizer.TryConsume(endToken))
                {
                    if (tokenizer.AtEnd)
                    {
                        throw tokenizer.CreateFormatException("Expected \"" + endToken + "\".");
                    }
                    MergeField(tokenizer, extensionRegistry, subBuilder);
                }

                value = subBuilder.WeakBuild();
            }
            else
            {
                tokenizer.Consume(":");

                switch (field.FieldType)
                {
                case FieldType.Int32:
                case FieldType.SInt32:
                case FieldType.SFixed32:
                    value = tokenizer.ConsumeInt32();
                    break;

                case FieldType.Int64:
                case FieldType.SInt64:
                case FieldType.SFixed64:
                    value = tokenizer.ConsumeInt64();
                    break;

                case FieldType.UInt32:
                case FieldType.Fixed32:
                    value = tokenizer.ConsumeUInt32();
                    break;

                case FieldType.UInt64:
                case FieldType.Fixed64:
                    value = tokenizer.ConsumeUInt64();
                    break;

                case FieldType.Float:
                    value = tokenizer.ConsumeFloat();
                    break;

                case FieldType.Double:
                    value = tokenizer.ConsumeDouble();
                    break;

                case FieldType.Bool:
                    value = tokenizer.ConsumeBoolean();
                    break;

                case FieldType.String:
                    value = tokenizer.ConsumeString();
                    break;

                case FieldType.Bytes:
                    value = tokenizer.ConsumeByteString();
                    break;

                case FieldType.Enum:
                {
                    EnumDescriptor enumType = field.EnumType;

                    if (tokenizer.LookingAtInteger())
                    {
                        int number = tokenizer.ConsumeInt32();
                        value = enumType.FindValueByNumber(number);
                        if (value == null)
                        {
                            throw tokenizer.CreateFormatExceptionPreviousToken(
                                      "Enum type \"" + enumType.FullName +
                                      "\" has no value with number " + number + ".");
                        }
                    }
                    else
                    {
                        String id = tokenizer.ConsumeIdentifier();
                        value = enumType.FindValueByName(id);
                        if (value == null)
                        {
                            throw tokenizer.CreateFormatExceptionPreviousToken(
                                      "Enum type \"" + enumType.FullName +
                                      "\" has no value named \"" + id + "\".");
                        }
                    }

                    break;
                }

                case FieldType.Message:
                case FieldType.Group:
                    throw new InvalidOperationException("Can't get here.");
                }
            }

            if (field.IsRepeated)
            {
                builder.WeakAddRepeatedField(field, value);
            }
            else
            {
                builder.SetField(field, value);
            }
        }
Beispiel #17
0
            /// <summary>
            /// Like <see cref="MergeFrom(CodedInputStream, ExtensionRegistry, IBuilder)" />
            /// but parses a single field.
            /// </summary>
            /// <param name="input">The input to read the field from</param>
            /// <param name="extensionRegistry">Registry to use when an extension field is encountered</param>
            /// <param name="builder">Builder to merge field into, if it's a known field</param>
            /// <param name="tag">The tag, which should already have been read from the input</param>
            /// <returns>true unless the tag is an end-group tag</returns>
            internal bool MergeFieldFrom(CodedInputStream input,
                                         ExtensionRegistry extensionRegistry, IBuilder builder, uint tag)
            {
                MessageDescriptor type = builder.DescriptorForType;

                if (type.Options.MessageSetWireFormat && tag == WireFormat.MessageSetTag.ItemStart)
                {
                    MergeMessageSetExtensionFromCodedStream(input, extensionRegistry, builder);
                    return(true);
                }

                WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
                int fieldNumber = WireFormat.GetTagFieldNumber(tag);

                FieldDescriptor field;
                IMessageLite    defaultFieldInstance = null;

                if (type.IsExtensionNumber(fieldNumber))
                {
                    ExtensionInfo extension = extensionRegistry[type, fieldNumber];
                    if (extension == null)
                    {
                        field = null;
                    }
                    else
                    {
                        field = extension.Descriptor;
                        defaultFieldInstance = extension.DefaultInstance;
                    }
                }
                else
                {
                    field = type.FindFieldByNumber(fieldNumber);
                }

                // Unknown field or wrong wire type. Skip.
                if (field == null || wireType != WireFormat.GetWireType(field))
                {
                    return(MergeFieldFrom(tag, input));
                }

                if (field.IsPacked)
                {
                    int length = (int)input.ReadRawVarint32();
                    int limit  = input.PushLimit(length);
                    if (field.FieldType == FieldType.Enum)
                    {
                        while (!input.ReachedLimit)
                        {
                            int    rawValue = input.ReadEnum();
                            object value    = field.EnumType.FindValueByNumber(rawValue);
                            if (value == null)
                            {
                                // If the number isn't recognized as a valid value for this
                                // enum, drop it (don't even add it to unknownFields).
                                return(true);
                            }
                            builder.WeakAddRepeatedField(field, value);
                        }
                    }
                    else
                    {
                        while (!input.ReachedLimit)
                        {
                            Object value = input.ReadPrimitiveField(field.FieldType);
                            builder.WeakAddRepeatedField(field, value);
                        }
                    }
                    input.PopLimit(limit);
                }
                else
                {
                    object value;
                    switch (field.FieldType)
                    {
                    case FieldType.Group:
                    case FieldType.Message: {
                        IBuilderLite subBuilder;
                        if (defaultFieldInstance != null)
                        {
                            subBuilder = defaultFieldInstance.WeakCreateBuilderForType();
                        }
                        else
                        {
                            subBuilder = builder.CreateBuilderForField(field);
                        }
                        if (!field.IsRepeated)
                        {
                            subBuilder.WeakMergeFrom((IMessageLite)builder[field]);
                        }
                        if (field.FieldType == FieldType.Group)
                        {
                            input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
                        }
                        else
                        {
                            input.ReadMessage(subBuilder, extensionRegistry);
                        }
                        value = subBuilder.WeakBuild();
                        break;
                    }

                    case FieldType.Enum: {
                        int rawValue = input.ReadEnum();
                        value = field.EnumType.FindValueByNumber(rawValue);
                        // If the number isn't recognized as a valid value for this enum,
                        // drop it.
                        if (value == null)
                        {
                            MergeVarintField(fieldNumber, (ulong)rawValue);
                            return(true);
                        }
                        break;
                    }

                    default:
                        value = input.ReadPrimitiveField(field.FieldType);
                        break;
                    }
                    if (field.IsRepeated)
                    {
                        builder.WeakAddRepeatedField(field, value);
                    }
                    else
                    {
                        builder[field] = value;
                    }
                }
                return(true);
            }
 private MessageStreamIterator(StreamProvider streamProvider, ExtensionRegistry extensionRegistry)
     : this(streamProvider, extensionRegistry, CodedInputStream.DefaultSizeLimit)
 {
 }
Beispiel #19
0
 IBuilderLite IBuilderLite.WeakMergeFrom(ByteString data, ExtensionRegistry registry)
 {
     return(MergeFrom(data));
 }
 /// <summary>
 /// Creates a new instance which uses the same stream provider as this one,
 /// but the specified extension registry.
 /// </summary>
 public MessageStreamIterator <TMessage> WithExtensionRegistry(ExtensionRegistry newRegistry)
 {
     return(new MessageStreamIterator <TMessage>(streamProvider, newRegistry, sizeLimit));
 }
Beispiel #21
0
 protected virtual bool ParseUnknownField(ICodedInputStream input,
                                          ExtensionRegistry extensionRegistry, uint tag, string fieldName)
 {
     return(input.SkipField());
 }
Beispiel #22
0
 IBuilder IBuilder.WeakMergeFrom(ICodedInputStream input, ExtensionRegistry registry)
 {
     return(MergeFrom(input, registry));
 }
Beispiel #23
0
 protected virtual bool ParseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
                                          ExtensionRegistry extensionRegistry, uint tag)
 {
     return(unknownFields.MergeFieldFrom(tag, input));
 }
Beispiel #24
0
        public void ExtensionWriterTest()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder()
                                                .SetExtension(UnitTestProtoFile.DefaultBoolExtension, true)
                                                .SetExtension(UnitTestProtoFile.DefaultBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(UnitTestProtoFile.DefaultCordExtension, "123")
                                                .SetExtension(UnitTestProtoFile.DefaultDoubleExtension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultFixed32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.DefaultFixed64Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.DefaultFloatExtension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(UnitTestProtoFile.DefaultImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(UnitTestProtoFile.DefaultInt32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultInt64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(UnitTestProtoFile.DefaultSfixed32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultSfixed64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultSint32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultSint64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultStringExtension, "123")
                                                .SetExtension(UnitTestProtoFile.DefaultStringPieceExtension, "123")
                                                .SetExtension(UnitTestProtoFile.DefaultUint32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.DefaultUint64Extension, 123u)
                                                //Optional
                                                .SetExtension(UnitTestProtoFile.OptionalBoolExtension, true)
                                                .SetExtension(UnitTestProtoFile.OptionalBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(UnitTestProtoFile.OptionalCordExtension, "123")
                                                .SetExtension(UnitTestProtoFile.OptionalDoubleExtension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalFixed32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.OptionalFixed64Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.OptionalFloatExtension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(UnitTestProtoFile.OptionalImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(UnitTestProtoFile.OptionalInt32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalInt64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(UnitTestProtoFile.OptionalSfixed32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalSfixed64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalSint32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalSint64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalStringExtension, "123")
                                                .SetExtension(UnitTestProtoFile.OptionalStringPieceExtension, "123")
                                                .SetExtension(UnitTestProtoFile.OptionalUint32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.OptionalUint64Extension, 123u)
                                                //Repeated
                                                .AddExtension(UnitTestProtoFile.RepeatedBoolExtension, true)
                                                .AddExtension(UnitTestProtoFile.RepeatedBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .AddExtension(UnitTestProtoFile.RepeatedCordExtension, "123")
                                                .AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedFixed32Extension, 123u)
                                                .AddExtension(UnitTestProtoFile.RepeatedFixed64Extension, 123u)
                                                .AddExtension(UnitTestProtoFile.RepeatedFloatExtension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .AddExtension(UnitTestProtoFile.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedInt64Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .AddExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedSint32Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedSint64Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedStringExtension, "123")
                                                .AddExtension(UnitTestProtoFile.RepeatedStringPieceExtension, "123")
                                                .AddExtension(UnitTestProtoFile.RepeatedUint32Extension, 123u)
                                                .AddExtension(UnitTestProtoFile.RepeatedUint64Extension, 123u)
            ;
            TestAllExtensions msg = builder.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestProtoFile.RegisterAllExtensions(registry);

            TestAllExtensions.Builder copyBuilder = TestAllExtensions.CreateBuilder().MergeFrom(msg.ToByteArray(), registry);
            TestAllExtensions         copy        = copyBuilder.Build();

            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());

            Assert.AreEqual(true, copy.GetExtension(UnitTestProtoFile.DefaultBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(UnitTestProtoFile.DefaultBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.DefaultCordExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(UnitTestProtoFile.DefaultForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(UnitTestProtoFile.DefaultImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, copy.GetExtension(UnitTestProtoFile.DefaultNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.DefaultStringExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.DefaultStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(UnitTestProtoFile.OptionalBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(UnitTestProtoFile.OptionalBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.OptionalCordExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(UnitTestProtoFile.OptionalForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(UnitTestProtoFile.OptionalImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, copy.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.OptionalStringExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.OptionalStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(UnitTestProtoFile.RepeatedBoolExtension, 0));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(UnitTestProtoFile.RepeatedBytesExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.RepeatedCordExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedDoubleExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedFixed32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedFixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedFloatExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, 0));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(UnitTestProtoFile.RepeatedImportEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedInt32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedInt64Extension, 0));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, copy.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSint32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSint64Extension, 0));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.RepeatedStringExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.RepeatedStringPieceExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedUint32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedUint64Extension, 0));
        }
    static int MergeFrom(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 2 && TypeChecker.CheckTypes <Google.ProtocolBuffers.IMessageLite>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                Google.ProtocolBuffers.IMessageLite arg0 = (Google.ProtocolBuffers.IMessageLite)ToLua.ToObject(L, 2);
                MyLib.GCPlayerCmd.Builder           o    = obj.MergeFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <Google.ProtocolBuffers.CodedInputStream>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                Google.ProtocolBuffers.CodedInputStream arg0 = (Google.ProtocolBuffers.CodedInputStream)ToLua.ToObject(L, 2);
                MyLib.GCPlayerCmd.Builder o = obj.MergeFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <Google.ProtocolBuffers.ByteString>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                Google.ProtocolBuffers.ByteString arg0 = (Google.ProtocolBuffers.ByteString)ToLua.ToObject(L, 2);
                MyLib.GCPlayerCmd.Builder         o    = obj.MergeFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <byte[]>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                byte[] arg0 = ToLua.CheckByteBuffer(L, 2);
                MyLib.GCPlayerCmd.Builder o = obj.MergeFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <System.IO.Stream>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                System.IO.Stream          arg0 = (System.IO.Stream)ToLua.ToObject(L, 2);
                MyLib.GCPlayerCmd.Builder o    = obj.MergeFrom(arg0);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <Google.ProtocolBuffers.CodedInputStream, Google.ProtocolBuffers.ExtensionRegistry>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                Google.ProtocolBuffers.CodedInputStream  arg0 = (Google.ProtocolBuffers.CodedInputStream)ToLua.ToObject(L, 2);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 3);
                MyLib.GCPlayerCmd.Builder o = obj.MergeFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <Google.ProtocolBuffers.ByteString, Google.ProtocolBuffers.ExtensionRegistry>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                Google.ProtocolBuffers.ByteString        arg0 = (Google.ProtocolBuffers.ByteString)ToLua.ToObject(L, 2);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 3);
                MyLib.GCPlayerCmd.Builder o = obj.MergeFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <byte[], Google.ProtocolBuffers.ExtensionRegistry>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                byte[] arg0 = ToLua.CheckByteBuffer(L, 2);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 3);
                MyLib.GCPlayerCmd.Builder o = obj.MergeFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <System.IO.Stream, Google.ProtocolBuffers.ExtensionRegistry>(L, 2))
            {
                Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> obj = (Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder>)ToLua.CheckObject <Google.ProtocolBuffers.AbstractBuilderLite <MyLib.GCPlayerCmd, MyLib.GCPlayerCmd.Builder> >(L, 1);
                System.IO.Stream arg0 = (System.IO.Stream)ToLua.ToObject(L, 2);
                Google.ProtocolBuffers.ExtensionRegistry arg1 = (Google.ProtocolBuffers.ExtensionRegistry)ToLua.ToObject(L, 3);
                MyLib.GCPlayerCmd.Builder o = obj.MergeFrom(arg0, arg1);
                ToLua.PushSealed(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: Google.ProtocolBuffers.AbstractBuilderLite<MyLib.GCPlayerCmd,MyLib.GCPlayerCmd.Builder>.MergeFrom"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
        /// <summary>
        /// Method we'll use to build messageReader, with the first parameter fixed to TMessage.CreateBuilder. Note that we
        /// have to introduce another type parameter (TMessage2) as we can't constrain TMessage for just a single method
        /// (and we can't do it at the type level because we don't know TBuilder). However, by constraining TMessage2
        /// to not only implement IMessage appropriately but also to derive from TMessage2, we can avoid doing a cast
        /// for every message; the implicit reference conversion will be fine. In practice, TMessage2 and TMessage will
        /// be the same type when we construct the generic method by reflection.
        /// </summary>
        private static TMessage BuildImpl <TMessage2, TBuilder>(Func <TBuilder> builderBuilder, CodedInputStream input, ExtensionRegistry registry)
            where TBuilder : IBuilder <TMessage2, TBuilder>
            where TMessage2 : TMessage, IMessage <TMessage2, TBuilder>
        {
            TBuilder builder = builderBuilder();

            input.ReadMessage(builder, registry);
            return(builder.Build());
        }