Exemplo n.º 1
0
        static unsafe void Main( string[] args )
        {
            Environment.ExitCode = 0;

            if ( args.Length == 0 )
            {
                Console.WriteLine( "No target specified." );

                Environment.ExitCode = -1;
                return;
            }

            string target = args[ 0 ];
            string output = ( ( args.Length > 1 ) ? args[ 1 ] : null );

            ImageFile imgFile = new ImageFile( target, output );

            try
            {
                imgFile.Process();
            }
            catch ( Exception ex )
            {
                Console.WriteLine( "Unable to process file: {0}", ex.Message );
                Environment.ExitCode = -1;
            }
        }
Exemplo n.º 2
0
        // because of protobuf-net we're limited to parsing simple types at run-time as we can't parse the protobuf, but options shouldn't be too complex
        private void BuildExtension(string key, FieldDescriptorProto[] fields)
        {
            TypeBuilder extension = moduleBuilder.DefineType(key + "Ext", TypeAttributes.Class);

            Type                   pcType    = typeof(ProtoContractAttribute);
            ConstructorInfo        pcCtor    = pcType.GetConstructor(Type.EmptyTypes);
            CustomAttributeBuilder pcBuilder = new CustomAttributeBuilder(pcCtor, Type.EmptyTypes);

            extension.SetCustomAttribute(pcBuilder);

            foreach (var field in fields)
            {
                DataFormat format;
                bool       buildEnumProxy;
                Type       fieldType = ImageFile.LookupBasicType(field.type, out format, out buildEnumProxy);

                FieldBuilder fbuilder = extension.DefineField(field.name, fieldType, FieldAttributes.Public);

                object defaultValue;
                if (String.IsNullOrEmpty(field.default_value))
                {
                    defaultValue = Activator.CreateInstance(fieldType);
                }
                else
                {
                    try
                    {
                        defaultValue = Convert.ChangeType(field.default_value, fieldType);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Constructor for extension had bad format: {0}", key);
                        return;
                    }
                }


                if (buildEnumProxy)
                {
                    Type                   epType    = typeof(EnumProxyAttribute);
                    ConstructorInfo        epCtor    = epType.GetConstructor(new Type[] { typeof(string) });
                    CustomAttributeBuilder epBuilder = new CustomAttributeBuilder(epCtor, new object[] { field.type_name });

                    fbuilder.SetCustomAttribute(epBuilder);
                }

                Type                   dvType    = typeof(ProtoDefaultValueAttribute);
                ConstructorInfo        dvCtor    = dvType.GetConstructor(new Type[] { typeof(object) });
                CustomAttributeBuilder dvBuilder = new CustomAttributeBuilder(dvCtor, new object[] { defaultValue });

                fbuilder.SetCustomAttribute(dvBuilder);

                Type                   pmType    = typeof(ProtoMemberAttribute);
                ConstructorInfo        pmCtor    = pmType.GetConstructor(new Type[] { typeof(int) });
                CustomAttributeBuilder pmBuilder = new CustomAttributeBuilder(pmCtor, new object[] { field.number },
                                                                              new PropertyInfo[] { pmType.GetProperty("Name"), pmType.GetProperty("DataFormat") },
                                                                              new object[] { "(" + field.name + ")", format });


                fbuilder.SetCustomAttribute(pmBuilder);
            }

            Type extensionType = extension.CreateType();

            if (!this.protobufExtensions.ContainsKey(key))
            {
                this.protobufExtensions[key] = new List <Type>();
            }

            this.protobufExtensions[key].Add(extensionType);
        }