Exemplo n.º 1
0
        /// <summary>
        /// Creates an instance for generated code.
        /// </summary>
        /// <remarks>
        /// The <paramref name="generatedCodeInfo"/> parameter should be null for descriptors which don't correspond to
        /// generated types. Otherwise, it should be a <see cref="GeneratedCodeInfo"/> with nested types and nested
        /// enums corresponding to the types and enums contained within the file descriptor.
        /// </remarks>
        public static FileDescriptor InternalBuildGeneratedFileFrom(byte[] descriptorData,
                                                                    FileDescriptor[] dependencies,
                                                                    GeneratedCodeInfo generatedCodeInfo)
        {
            FileDescriptorProto proto;

            try
            {
                proto = FileDescriptorProto.Parser.ParseFrom(descriptorData);
            }
            catch (InvalidProtocolBufferException e)
            {
                throw new ArgumentException("Failed to parse protocol buffer descriptor for generated code.", e);
            }



            try
            {
                // When building descriptors for generated code, we allow unknown
                // dependencies by default.
                return(BuildFrom(ByteString.CopyFrom(descriptorData), proto, dependencies, true, generatedCodeInfo));
            }
            catch (DescriptorValidationException e)
            {
                throw new ArgumentException("Invalid embedded descriptor for \"" + proto.Name + "\".", e);
            }
        }
Exemplo n.º 2
0
        private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, IEnumerable <FileDescriptor> dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo)
        {
            SerializedData = descriptorData;
            DescriptorPool = pool;
            Proto          = proto;
            Dependencies   = new ReadOnlyCollection <FileDescriptor>(dependencies.ToList());

            PublicDependencies = DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies);

            pool.AddPackage(Package, this);

            MessageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType,
                                                                 (message, index) =>
                                                                 new MessageDescriptor(message, this, null, index, generatedCodeInfo?.NestedTypes[index]));

            EnumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType,
                                                              (enumType, index) =>
                                                              new EnumDescriptor(enumType, this, null, index, generatedCodeInfo?.NestedEnums[index]));

            Services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service,
                                                             (service, index) =>
                                                             new ServiceDescriptor(service, this, index));

            declarations = new Lazy <Dictionary <IDescriptor, DescriptorDeclaration> >(CreateDeclarationMap, LazyThreadSafetyMode.ExecutionAndPublication);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Builds a FileDescriptor from its protocol buffer representation.
        /// </summary>
        /// <param name="descriptorData">The original serialized descriptor data.
        /// We have only limited proto2 support, so serializing FileDescriptorProto
        /// would not necessarily give us this.</param>
        /// <param name="proto">The protocol message form of the FileDescriptor.</param>
        /// <param name="dependencies">FileDescriptors corresponding to all of the
        /// file's dependencies, in the exact order listed in the .proto file. May be null,
        /// in which case it is treated as an empty array.</param>
        /// <param name="allowUnknownDependencies">Whether unknown dependencies are ignored (true) or cause an exception to be thrown (false).</param>
        /// <param name="generatedCodeInfo">Details about generated code, for the purposes of reflection.</param>
        /// <exception cref="DescriptorValidationException">If <paramref name="proto"/> is not
        /// a valid descriptor. This can occur for a number of reasons, such as a field
        /// having an undefined type or because two messages were defined with the same name.</exception>
        private static FileDescriptor BuildFrom(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo)
        {
            // Building descriptors involves two steps: translating and linking.
            // In the translation step (implemented by FileDescriptor's
            // constructor), we build an object tree mirroring the
            // FileDescriptorProto's tree and put all of the descriptors into the
            // DescriptorPool's lookup tables.  In the linking step, we look up all
            // type references in the DescriptorPool, so that, for example, a
            // FieldDescriptor for an embedded message contains a pointer directly
            // to the Descriptor for that message's type.  We also detect undefined
            // types in the linking step.
            if (dependencies == null)
            {
                dependencies = new FileDescriptor[0];
            }

            DescriptorPool pool   = new DescriptorPool(dependencies);
            FileDescriptor result = new FileDescriptor(descriptorData, proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo);

            // Validate that the dependencies we've been passed (as FileDescriptors) are actually the ones we
            // need.
            if (dependencies.Length != proto.Dependency.Count)
            {
                throw new DescriptorValidationException(
                          result,
                          "Dependencies passed to FileDescriptor.BuildFrom() don't match " +
                          "those listed in the FileDescriptorProto.");
            }

            result.CrossLink();
            return(result);
        }
Exemplo n.º 4
0
        private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
        {
            FileDescriptor __4__this;

            while (true)
            {
IL_113:
                uint arg_E7_0 = 3109136426u;
                while (true)
                {
                    uint num;
                    switch ((num = (arg_E7_0 ^ 2557234891u)) % 8u)
                    {
                    case 0u:
                        pool.AddPackage(this.Package, this);
                        arg_E7_0 = (num * 1105332455u ^ 1354664894u);
                        continue;

                    case 1u:
                        __4__this = this;
                        arg_E7_0  = (num * 4125576103u ^ 2241118048u);
                        continue;

                    case 2u:
                        this.< Dependencies > k__BackingField       = new ReadOnlyCollection <FileDescriptor>((FileDescriptor[])FileDescriptor.smethod_0(dependencies));
                        this.< PublicDependencies > k__BackingField = FileDescriptor.DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies);
                        arg_E7_0 = (num * 2070285500u ^ 1583842795u);
                        continue;

                    case 3u:
                        goto IL_113;

                    case 4u:
                        this.< SerializedData > k__BackingField = descriptorData;
                        arg_E7_0 = (num * 559784837u ^ 3768172688u);
                        continue;

                    case 5u:
                        this.< MessageTypes > k__BackingField = DescriptorUtil.ConvertAndMakeReadOnly <DescriptorProto, MessageDescriptor>(proto.MessageType, (DescriptorProto message, int index) => new MessageDescriptor(message, __4__this, null, index, generatedCodeInfo.NestedTypes[index]));
                        arg_E7_0 = (num * 3261531694u ^ 3702070131u);
                        continue;

                    case 7u:
                        this.< DescriptorPool > k__BackingField = pool;
                        this.< Proto > k__BackingField          = proto;
                        arg_E7_0 = (num * 2550378934u ^ 3723757811u);
                        continue;
                    }
                    goto Block_1;
                }
            }
Block_1:
            this.< EnumTypes > k__BackingField = DescriptorUtil.ConvertAndMakeReadOnly <EnumDescriptorProto, EnumDescriptor>(proto.EnumType, (EnumDescriptorProto enumType, int index) => new EnumDescriptor(enumType, __4__this, null, index, generatedCodeInfo.NestedEnums[index]));
            this.< Services > k__BackingField  = DescriptorUtil.ConvertAndMakeReadOnly <ServiceDescriptorProto, ServiceDescriptor>(proto.Service, (ServiceDescriptorProto service, int index) => new ServiceDescriptor(service, this, index));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Builds a FileDescriptor from its protocol buffer representation.
        /// </summary>
        /// <param name="descriptorData">The original serialized descriptor data.
        /// We have only limited proto2 support, so serializing FileDescriptorProto
        /// would not necessarily give us this.</param>
        /// <param name="proto">The protocol message form of the FileDescriptor.</param>
        /// <param name="dependencies">FileDescriptors corresponding to all of the
        /// file's dependencies, in the exact order listed in the .proto file. May be null,
        /// in which case it is treated as an empty array.</param>
        /// <param name="allowUnknownDependencies">Whether unknown dependencies are ignored (true) or cause an exception to be thrown (false).</param>
        /// <param name="generatedCodeInfo">Reflection information, if any. May be null, specifically for non-generated code.</param>
        /// <exception cref="DescriptorValidationException">If <paramref name="proto"/> is not
        /// a valid descriptor. This can occur for a number of reasons, such as a field
        /// having an undefined type or because two messages were defined with the same name.</exception>
        private static FileDescriptor BuildFrom(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
        {
            // Building descriptors involves two steps: translating and linking.
            // In the translation step (implemented by FileDescriptor's
            // constructor), we build an object tree mirroring the
            // FileDescriptorProto's tree and put all of the descriptors into the
            // DescriptorPool's lookup tables.  In the linking step, we look up all
            // type references in the DescriptorPool, so that, for example, a
            // FieldDescriptor for an embedded message contains a pointer directly
            // to the Descriptor for that message's type.  We also detect undefined
            // types in the linking step.
            if (dependencies == null)
            {
                dependencies = new FileDescriptor[0];
            }

            DescriptorPool pool   = new DescriptorPool(dependencies);
            FileDescriptor result = new FileDescriptor(descriptorData, proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo);

            // TODO(jonskeet): Reinstate these checks, or get rid of them entirely. They aren't in the Java code,
            // and fail for the CustomOptions test right now. (We get "descriptor.proto" vs "google/protobuf/descriptor.proto".)
            //if (dependencies.Length != proto.DependencyCount)
            //{
            //    throw new DescriptorValidationException(result,
            //                                            "Dependencies passed to FileDescriptor.BuildFrom() don't match " +
            //                                            "those listed in the FileDescriptorProto.");
            //}
            //for (int i = 0; i < proto.DependencyCount; i++)
            //{
            //    if (dependencies[i].Name != proto.DependencyList[i])
            //    {
            //        throw new DescriptorValidationException(result,
            //                                                "Dependencies passed to FileDescriptor.BuildFrom() don't match " +
            //                                                "those listed in the FileDescriptorProto.");
            //    }
            //}

            result.CrossLink();
            return(result);
        }
Exemplo n.º 6
0
        private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
        {
            this.descriptorData = descriptorData;
            this.pool           = pool;
            this.proto          = proto;
            this.dependencies   = new ReadOnlyCollection <FileDescriptor>((FileDescriptor[])dependencies.Clone());

            publicDependencies = DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies);

            pool.AddPackage(Package, this);

            messageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType,
                                                                 (message, index) =>
                                                                 new MessageDescriptor(message, this, null, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedTypes[index]));

            enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType,
                                                              (enumType, index) =>
                                                              new EnumDescriptor(enumType, this, null, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index]));

            services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service,
                                                             (service, index) =>
                                                             new ServiceDescriptor(service, this, index));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Extracts public dependencies from direct dependencies. This is a static method despite its
        /// first parameter, as the value we're in the middle of constructing is only used for exceptions.
        /// </summary>
        private static IList <FileDescriptor> DeterminePublicDependencies(FileDescriptor @this, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies)
        {
            var nameToFileMap = new Dictionary <string, FileDescriptor>();

            foreach (var file in dependencies)
            {
                nameToFileMap[file.Name] = file;
            }
            var publicDependencies = new List <FileDescriptor>();

            for (int i = 0; i < proto.PublicDependency.Count; i++)
            {
                int index = proto.PublicDependency[i];
                if (index < 0 || index >= proto.Dependency.Count)
                {
                    throw new DescriptorValidationException(@this, "Invalid public dependency index.");
                }
                string         name = proto.Dependency[index];
                FileDescriptor file = nameToFileMap[name];
                if (file == null)
                {
                    if (!allowUnknownDependencies)
                    {
                        throw new DescriptorValidationException(@this, "Invalid public dependency: " + name);
                    }
                    // Ignore unknown dependencies.
                }
                else
                {
                    publicDependencies.Add(file);
                }
            }
            return(new ReadOnlyCollection <FileDescriptor>(publicDependencies));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Extracts public dependencies from direct dependencies. This is a static method despite its
        /// first parameter, as the value we're in the middle of constructing is only used for exceptions.
        /// </summary>
        private static IList <FileDescriptor> DeterminePublicDependencies(FileDescriptor @this, FileDescriptorProto proto, IEnumerable <FileDescriptor> dependencies, bool allowUnknownDependencies)
        {
            var nameToFileMap      = dependencies.ToDictionary(file => file.Name);
            var publicDependencies = new List <FileDescriptor>();

            for (int i = 0; i < proto.PublicDependency.Count; i++)
            {
                int index = proto.PublicDependency[i];
                if (index < 0 || index >= proto.Dependency.Count)
                {
                    throw new DescriptorValidationException(@this, "Invalid public dependency index.");
                }
                string         name = proto.Dependency[index];
                FileDescriptor file;
                if (!nameToFileMap.TryGetValue(name, out file))
                {
                    if (!allowUnknownDependencies)
                    {
                        throw new DescriptorValidationException(@this, "Invalid public dependency: " + name);
                    }
                    // Ignore unknown dependencies.
                }
                else
                {
                    publicDependencies.Add(file);
                }
            }
            return(new ReadOnlyCollection <FileDescriptor>(publicDependencies));
        }
Exemplo n.º 9
0
        private static FileDescriptor BuildFrom(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
        {
            if (dependencies == null)
            {
                goto IL_18;
            }
            goto IL_122;
            uint           arg_D3_0;
            FileDescriptor fileDescriptor;
            int            num2;

            while (true)
            {
IL_CE:
                uint num;
                switch ((num = (arg_D3_0 ^ 4161713641u)) % 12u)
                {
                case 0u:
                    fileDescriptor.CrossLink();
                    arg_D3_0 = (num * 1953233347u ^ 2707760628u);
                    continue;

                case 1u:
                    num2     = 0;
                    arg_D3_0 = 2574715432u;
                    continue;

                case 2u:
                    num2++;
                    arg_D3_0 = 4243801185u;
                    continue;

                case 3u:
                    dependencies = new FileDescriptor[0];
                    arg_D3_0     = (num * 3942966366u ^ 4286440245u);
                    continue;

                case 4u:
                    arg_D3_0 = ((num2 < proto.Dependency.Count) ? 2982469083u : 3757505837u);
                    continue;

                case 5u:
                    arg_D3_0 = (num * 314203903u ^ 1276877662u);
                    continue;

                case 6u:
                    arg_D3_0 = ((!FileDescriptor.smethod_5(dependencies[num2].Name, proto.Dependency[num2])) ? 3773742995u : 2893382989u);
                    continue;

                case 7u:
                    goto IL_18;

                case 8u:
                    goto IL_14B;

                case 10u:
                    goto IL_122;

                case 11u:
                    goto IL_183;
                }
                break;
            }
            return(fileDescriptor);

IL_14B:
            throw new DescriptorValidationException(fileDescriptor, FileDescriptor.smethod_6(Module.smethod_37 <string>(2509052754u), proto.Dependency[num2], Module.smethod_35 <string>(3048061080u), dependencies[num2].Name));
IL_183:
            throw new DescriptorValidationException(fileDescriptor, Module.smethod_33 <string>(2639691648u));
IL_18:
            arg_D3_0 = 3320637418u;
            goto IL_CE;
IL_122:
            DescriptorPool pool = new DescriptorPool(dependencies);

            fileDescriptor = new FileDescriptor(descriptorData, proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo);
            arg_D3_0       = ((dependencies.Length != proto.Dependency.Count) ? 2984046962u : 2591580144u);
            goto IL_CE;
        }
Exemplo n.º 10
0
        private static IList <FileDescriptor> DeterminePublicDependencies(FileDescriptor @this, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies)
        {
            Dictionary <string, FileDescriptor> dictionary = new Dictionary <string, FileDescriptor>();
            List <FileDescriptor> list;
            string text;

            while (true)
            {
IL_25F:
                uint arg_1FD_0 = 1480753851u;
                while (true)
                {
                    uint num;
                    switch ((num = (arg_1FD_0 ^ 354656998u)) % 21u)
                    {
                    case 0u:
                    {
                        int num2;
                        arg_1FD_0 = ((num2 < dependencies.Length) ? 755598000u : 1625878074u);
                        continue;
                    }

                    case 1u:
                    {
                        FileDescriptor fileDescriptor;
                        arg_1FD_0 = (((fileDescriptor != null) ? 3503588187u : 2689291383u) ^ num * 269180022u);
                        continue;
                    }

                    case 2u:
                        arg_1FD_0 = (num * 1428810503u ^ 597188103u);
                        continue;

                    case 3u:
                    {
                        int num2;
                        num2++;
                        arg_1FD_0 = (num * 4026010881u ^ 1873699178u);
                        continue;
                    }

                    case 4u:
                    {
                        list = new List <FileDescriptor>();
                        int num3 = 0;
                        arg_1FD_0 = (num * 172851441u ^ 2057392629u);
                        continue;
                    }

                    case 5u:
                    {
                        int num4;
                        text      = proto.Dependency[num4];
                        arg_1FD_0 = 1182864162u;
                        continue;
                    }

                    case 6u:
                    {
                        int num4;
                        arg_1FD_0 = (((num4 < proto.Dependency.Count) ? 199583157u : 1553389033u) ^ num * 904630291u);
                        continue;
                    }

                    case 7u:
                    {
                        int num2 = 0;
                        arg_1FD_0 = (num * 2059016605u ^ 2886768975u);
                        continue;
                    }

                    case 8u:
                    {
                        FileDescriptor fileDescriptor2;
                        dictionary[fileDescriptor2.Name] = fileDescriptor2;
                        arg_1FD_0 = (num * 2415469140u ^ 158078469u);
                        continue;
                    }

                    case 9u:
                    {
                        FileDescriptor fileDescriptor = dictionary[text];
                        arg_1FD_0 = (num * 1703034579u ^ 4134718755u);
                        continue;
                    }

                    case 10u:
                    {
                        int num3;
                        num3++;
                        arg_1FD_0 = 1400639947u;
                        continue;
                    }

                    case 11u:
                        goto IL_266;

                    case 13u:
                        goto IL_277;

                    case 14u:
                    {
                        int num3;
                        int num4 = proto.PublicDependency[num3];
                        arg_1FD_0 = ((num4 < 0) ? 43381538u : 749549455u);
                        continue;
                    }

                    case 15u:
                        arg_1FD_0 = (num * 1315834947u ^ 1640480294u);
                        continue;

                    case 16u:
                        arg_1FD_0 = (((!allowUnknownDependencies) ? 1742178966u : 1225729944u) ^ num * 865084290u);
                        continue;

                    case 17u:
                    {
                        FileDescriptor fileDescriptor;
                        list.Add(fileDescriptor);
                        arg_1FD_0 = 83744118u;
                        continue;
                    }

                    case 18u:
                    {
                        int            num2;
                        FileDescriptor fileDescriptor2 = dependencies[num2];
                        arg_1FD_0 = 266579083u;
                        continue;
                    }

                    case 19u:
                        goto IL_25F;

                    case 20u:
                    {
                        int num3;
                        arg_1FD_0 = ((num3 < proto.PublicDependency.Count) ? 1502489908u : 1152782524u);
                        continue;
                    }
                    }
                    goto Block_7;
                }
            }
Block_7:
            goto IL_28F;
IL_266:
            throw new DescriptorValidationException(@this, Module.smethod_34 <string>(2614455357u));
IL_277:
            throw new DescriptorValidationException(@this, FileDescriptor.smethod_3(Module.smethod_35 <string>(82095339u), text));
IL_28F:
            return(new ReadOnlyCollection <FileDescriptor>(list));
        }