Esempio n. 1
0
 public void InitializeFrom(MetaDataOptions options)
 {
     MetaDataHeaderOptions.InitializeFrom(options.MetaDataHeaderOptions);
     TablesHeapOptions.InitializeFrom(options.TablesHeapOptions);
     Flags = options.Flags;
     OnFlagsChanged();
 }
Esempio n. 2
0
        public override void Finalize(RPContext ctx)
        {
            foreach (KeyValuePair <Tuple <Code, IMethod, IRPEncoding>, Tuple <FieldDef, MethodDef> > pair in this.fields)
            {
                byte           num;
                InitMethodDesc initMethod = this.GetInitMethod(ctx, pair.Key.Item3);
                do
                {
                    num = ctx.Random.NextByte();
                }while (num == ((byte)pair.Key.Item1));
                MethodDef def2 = pair.Value.Item1.DeclaringType.FindOrCreateStaticConstructor();
                def2.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, (IMethod)initMethod.Method));
                def2.Body.Instructions.Insert(0, Instruction.CreateLdcI4(num));
                def2.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldtoken, pair.Value.Item1));
                FieldDesc item = new FieldDesc {
                    Field    = pair.Value.Item1,
                    OpCode   = pair.Key.Item1,
                    Method   = pair.Key.Item2,
                    OpKey    = num,
                    InitDesc = initMethod
                };
                this.fieldDescs.Add(item);
            }
            foreach (TypeDef def3 in ctx.Delegates.Values)
            {
                MethodDef member = def3.FindOrCreateStaticConstructor();
                ctx.Marker.Mark(member, ctx.Protection);
                ctx.Name.SetCanRename(member, false);
            }
            MetaDataOptions metaDataOptions = ctx.Context.CurrentModuleWriterOptions.MetaDataOptions;

            metaDataOptions.Flags |= MetaDataFlags.PreserveExtraSignatureData;
            ctx.Context.CurrentModuleWriterListener.OnWriterEvent += new EventHandler <ModuleWriterListenerEventArgs>(this.EncodeField);
            this.encodeCtx = ctx;
        }
 /// <inheritdoc />
 public IDatabaseSchema GetDatabaseSchema(MetaDataOptions options = null)
 {
     if (options == null)
     {
         options = new MetaDataOptions();
     }
     using (MetaDataBuilder mb = new MetaDataBuilder(this, options))
     {
         return(mb.BuildDatabaseSchema());
     }
 }
        /// <inheritdoc />
        public void ExportDatabaseSchema(string fileName, MetaDataOptions options = null)
        {
            if (options == null)
            {
                options = new MetaDataOptions();
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("Filename null or empty.", nameof(fileName));
            }

            using (ICommenceDatabase db = new CommenceDatabase())
            {
                var schema = db.GetDatabaseSchema(options);

                switch (options.Format)
                {
                default:
                case MetaDataFormat.Json:
                    var s = JsonConvert.SerializeObject(schema);
                    using (StreamWriter sw = new StreamWriter(fileName))
                    {
                        sw.Write(s);
                    }
                    break;

                case MetaDataFormat.Xml:

                    XmlSerializer xsSubmit = new XmlSerializer(typeof(DatabaseSchema));
                    var           xml      = string.Empty;

                    using (var sw = new StreamWriter(fileName))
                    {
                        using (XmlWriter writer = XmlWriter.Create(sw))
                        {
                            xsSubmit.Serialize(writer, schema);
                            xml = sw.ToString();     // Your XML
                        }
                    }
                    break;
                }
            }
        }
Esempio n. 5
0
        public static AssemblyDefinition Translate(ModuleDefMD manifestModule)
        {
            using (var assemblyStream = new MemoryStream())
            {
                try
                {
                    if (manifestModule.IsILOnly)
                    {
                        var writerOptions = new ModuleWriterOptions(manifestModule);
                        writerOptions.Logger = DummyLogger.NoThrowInstance;

                        MetaDataOptions metaDataOptions = new MetaDataOptions();
                        metaDataOptions.Flags = MetaDataFlags.PreserveAll;

                        manifestModule.Write(assemblyStream, writerOptions);
                    }
                    else
                    {
                        var writerOptions = new NativeModuleWriterOptions(manifestModule);
                        writerOptions.Logger = DummyLogger.NoThrowInstance;

                        MetaDataOptions metaDataOptions = new MetaDataOptions();
                        metaDataOptions.Flags = MetaDataFlags.PreserveAll;

                        manifestModule.NativeWrite(assemblyStream, writerOptions);
                    }
                }
                catch (Exception)
                {
                    if (assemblyStream.Length == 0)
                    {
                        return(null);
                    }
                }

                assemblyStream.Position = 0;
                AssemblyDefinition newAssembly = AssemblyDefinition.ReadAssembly(assemblyStream);

                return(newAssembly);
            }
        }
Esempio n. 6
0
        public static AssemblyDefinition Translate(ModuleDefMD manifestModule)
        {
            using (var assemblyStream = new MemoryStream())
            {
                try
                {
                    if (manifestModule.IsILOnly)
                    {
                        var writerOptions = new ModuleWriterOptions(manifestModule);
                        writerOptions.Logger = DummyLogger.NoThrowInstance;

                        MetaDataOptions metaDataOptions = new MetaDataOptions();
                        metaDataOptions.Flags = MetaDataFlags.PreserveAll;

                        manifestModule.Write(assemblyStream, writerOptions);
                    }
                    else
                    {
                        var writerOptions = new NativeModuleWriterOptions(manifestModule);
                        writerOptions.Logger = DummyLogger.NoThrowInstance;

                        MetaDataOptions metaDataOptions = new MetaDataOptions();
                        metaDataOptions.Flags = MetaDataFlags.PreserveAll;

                        manifestModule.NativeWrite(assemblyStream, writerOptions);
                    }
                }
                catch (Exception)
                {
                    if (assemblyStream.Length == 0)
                        return null;
                }

                assemblyStream.Position = 0;
                AssemblyDefinition newAssembly = AssemblyDefinition.ReadAssembly(assemblyStream);

                return newAssembly;
            }
        }
Esempio n. 7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Module</param>
 /// <param name="constants">Constants list</param>
 /// <param name="methodBodies">Method bodies list</param>
 /// <param name="netResources">.NET resources list</param>
 /// <param name="options">Options</param>
 public NormalMetaData(ModuleDef module, UniqueChunkList <ByteArrayChunk> constants, MethodBodyChunks methodBodies, NetResources netResources, MetaDataOptions options)
     : base(module, constants, methodBodies, netResources, options)
 {
 }
Esempio n. 8
0
 public NormalMetaData(ModuleDef module, UniqueChunkList <ByteArrayChunk> constants, MethodBodyChunks methodBodies, NetResources netResources, MetaDataOptions options, DebugMetaDataKind debugKind, bool isStandaloneDebugMetadata)
     : base(module, constants, methodBodies, netResources, options, debugKind, isStandaloneDebugMetadata)
 {
 }
Esempio n. 9
0
 public void CopyTo(MetaDataOptions options)
 {
     MetaDataHeaderOptions.CopyTo(options.MetaDataHeaderOptions);
     TablesHeapOptions.CopyTo(options.TablesHeapOptions);
     options.Flags = Flags;
 }
Esempio n. 10
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="module">Module</param>
		/// <param name="constants">Constants list</param>
		/// <param name="methodBodies">Method bodies list</param>
		/// <param name="netResources">.NET resources list</param>
		/// <param name="options">Options</param>
		public NormalMetaData(ModuleDef module, UniqueChunkList<ByteArrayChunk> constants, MethodBodyChunks methodBodies, NetResources netResources, MetaDataOptions options)
			: base(module, constants, methodBodies, netResources, options) {
		}
Esempio n. 11
0
		public void InitializeFrom(MetaDataOptions options) {
			MetaDataHeaderOptions.InitializeFrom(options.MetaDataHeaderOptions);
			TablesHeapOptions.InitializeFrom(options.TablesHeapOptions);
			Flags = options.Flags;
			OnFlagsChanged();
		}
Esempio n. 12
0
		public void CopyTo(MetaDataOptions options) {
			MetaDataHeaderOptions.CopyTo(options.MetaDataHeaderOptions);
			TablesHeapOptions.CopyTo(options.TablesHeapOptions);
			options.Flags = Flags;
		}
Esempio n. 13
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtPath.Text))
            {
                return;
            }

            try
            {
                if (_module.IsILOnly)
                {
                    ModuleWriterOptions writer = new ModuleWriterOptions(_module);

                    if (chkNoThrowInstanceLogger.Checked)
                    {
                        writer.Logger = DummyLogger.NoThrowInstance;
                    }

                    MetaDataOptions metaDataOptions = writer.MetaDataOptions;

                    #region MetaDataSetters

                    if (chkPreserveTypeRefRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveTypeRefRids;
                    }
                    if (chkPreserveTypeDefRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveTypeDefRids;
                    }
                    if (chkPreserveFieldRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveFieldRids;
                    }
                    if (chkPreserveMethodRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveMethodRids;
                    }
                    if (chkPreserveParamRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveParamRids;
                    }
                    if (chkPreserveMemberRefRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveMemberRefRids;
                    }
                    if (chkPreserveStandAloneSigRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveStandAloneSigRids;
                    }
                    if (chkPreserveEventRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveEventRids;
                    }
                    if (chkPreservePropertyRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreservePropertyRids;
                    }
                    if (chkPreserveTypeSpecRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveTypeSpecRids;
                    }
                    if (chkPreserveMethodSpecRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveMethodSpecRids;
                    }
                    if (chkPreserveAllMethodRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveAllMethodRids;
                    }
                    if (chkPreserveRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveRids;
                    }
                    if (chkPreserveStringOffsets.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveStringsOffsets;
                    }
                    if (chkPreserveUSOffsets.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveUSOffsets;
                    }
                    if (chkPreserveBlobOffsets.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveBlobOffsets;
                    }
                    if (chkPreserveExtraSignatureData.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveExtraSignatureData;
                    }
                    if (chkPreserveAll.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveAll;
                    }
                    if (chkKeepOldMaxStack.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.KeepOldMaxStack;
                    }
                    if (chkAlwaysCreateGuidHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateGuidHeap;
                    }
                    if (chkAlwaysCreateStringsHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateStringsHeap;
                    }
                    if (chkAlwaysCreateUSHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateUSHeap;
                    }
                    if (chkAlwaysCreateBlobHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateBlobHeap;
                    }

                    #endregion

                    _module.Write(txtPath.Text, writer);
                }
                else
                {
                    NativeModuleWriterOptions writer = new NativeModuleWriterOptions(_module);

                    if (chkNoThrowInstanceLogger.Checked)
                    {
                        writer.Logger = DummyLogger.NoThrowInstance;
                    }

                    MetaDataOptions metaDataOptions = writer.MetaDataOptions;

                    #region MetaDataSetters

                    if (chkPreserveTypeRefRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveTypeRefRids;
                    }
                    if (chkPreserveTypeDefRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveTypeDefRids;
                    }
                    if (chkPreserveFieldRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveFieldRids;
                    }
                    if (chkPreserveMethodRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveMethodRids;
                    }
                    if (chkPreserveParamRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveParamRids;
                    }
                    if (chkPreserveMemberRefRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveMemberRefRids;
                    }
                    if (chkPreserveStandAloneSigRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveStandAloneSigRids;
                    }
                    if (chkPreserveEventRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveEventRids;
                    }
                    if (chkPreservePropertyRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreservePropertyRids;
                    }
                    if (chkPreserveTypeSpecRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveTypeSpecRids;
                    }
                    if (chkPreserveMethodSpecRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveMethodSpecRids;
                    }
                    if (chkPreserveAllMethodRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveAllMethodRids;
                    }
                    if (chkPreserveRids.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveRids;
                    }
                    if (chkPreserveStringOffsets.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveStringsOffsets;
                    }
                    if (chkPreserveUSOffsets.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveUSOffsets;
                    }
                    if (chkPreserveBlobOffsets.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveBlobOffsets;
                    }
                    if (chkPreserveExtraSignatureData.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveExtraSignatureData;
                    }
                    if (chkPreserveAll.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.PreserveAll;
                    }
                    if (chkKeepOldMaxStack.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.KeepOldMaxStack;
                    }
                    if (chkAlwaysCreateGuidHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateGuidHeap;
                    }
                    if (chkAlwaysCreateStringsHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateStringsHeap;
                    }
                    if (chkAlwaysCreateUSHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateUSHeap;
                    }
                    if (chkAlwaysCreateBlobHeap.Checked)
                    {
                        metaDataOptions.Flags = metaDataOptions.Flags | MetaDataFlags.AlwaysCreateBlobHeap;
                    }

                    #endregion

                    _module.NativeWrite(txtPath.Text, writer);
                }
            }
            catch (Exception o)
            {
                MessageBox.Show("Could not write assembly!" + Environment.NewLine + Environment.NewLine + o.Message,
                                "Error");
                return;
            }

            MessageBox.Show("Assembly written to:" + Environment.NewLine + txtPath.Text, "Success");

            Close();
        }