示例#1
0
 private void Embed(ModuleDefMD2 targetModule, AssemblyFile assemblyFile)
 {
     using (var moduleManager = new ModuleManager(assemblyFile.Path, false, null))
     {
         var baseName = GetBaseName(moduleManager.Module);
         Logging.Write("Embedding {0} from {1}", baseName, assemblyFile.Path);
         var gzippedAssembly = GetGZippedAssembly(assemblyFile.Path);
         targetModule.Resources.Add(new EmbeddedResource(Loader.GetEmbeddedAssemblyResourceName(baseName), gzippedAssembly));
     }
     assemblyFile.DeleteIfLocal();
 }
示例#2
0
 public ModuleRelocator(ModuleDefMD2 oldModule, ModuleDefMD2 newModule)
     : base(newModule)
 {
     _oldModule = oldModule;
     _newModule = newModule;
 }
示例#3
0
 /// <summary>
 /// Indicates whether the given <see cref="AssemblyRef"/> belongs to given module
 /// </summary>
 /// <param name="assemblyScope">The assembly scope.</param>
 /// <param name="module">The module.</param>
 /// <returns></returns>
 public static bool BelongsTo(this AssemblyRef assemblyScope, ModuleDefMD2 module)
 {
     return(assemblyScope?.FullName == module.Assembly.FullName);
 }
示例#4
0
 /// <summary>
 /// Indicates whether the given <see cref="TypeSig" /> belongs to given module
 /// </summary>
 /// <param name="typeDefOrRef">The type definition or reference.</param>
 /// <param name="module">The module.</param>
 /// <returns></returns>
 public static bool BelongsTo(this ITypeDefOrRef typeDefOrRef, ModuleDefMD2 module) => BelongsTo(typeDefOrRef.Scope as AssemblyRef, module);
示例#5
0
 /// <summary>
 /// Indicates whether the given <see cref="TypeSig" /> belongs to given module
 /// </summary>
 /// <param name="typeSig">The type sig.</param>
 /// <param name="module">The module.</param>
 /// <returns></returns>
 public static bool BelongsTo(this TypeSig typeSig, ModuleDefMD2 module) => BelongsTo(typeSig.Scope as AssemblyRef, module);
示例#6
0
 protected Relocator(ModuleDefMD2 targetModule)
 {
     _targetModule = targetModule;
 }
示例#7
0
        /// <summary>
        /// Merges the specified target module.
        /// </summary>
        /// <param name="targetModule">The target module.</param>
        /// <param name="assemblyFile">The assembly file.</param>
        private IDisposable Merge(ModuleDefMD2 targetModule, AssemblyFile assemblyFile)
        {
            var moduleManager = new ModuleManager(assemblyFile.Path, false, null);

            {
                var baseName = GetBaseName(moduleManager.Module);
                Logging.Write("Merging   {0}", baseName);
                targetModule.Resources.Add(new EmbeddedResource(Loader.GetMergedAssemblyResourceName(baseName), new byte[0]));

                var allReferenceTypes = moduleManager.Module.Types.ToArray();
                moduleManager.Module.Types.Clear();
                foreach (var referenceType in allReferenceTypes)
                {
                    // <Module> is handled differently
                    if (referenceType.Name == "<Module>")
                    {
                        var referenceCCtor = referenceType.FindStaticConstructor();
                        // if there is a cctor in ref
                        if (referenceCCtor != null)
                        {
                            // if no target <Module> (I don't think this is even possible), then the current <Module> is copied
                            var targetModuleModuleType = targetModule.Find("<Module>", true);
                            if (targetModuleModuleType == null)
                            {
                                targetModule.Types.Add(referenceType);
                            }
                            else
                            {
                                // otherwise the ref cctor is renamed, inserted as a simple method and called
                                var targetModuleCctor = targetModuleModuleType.FindOrCreateStaticConstructor();
                                // 1. renaming
                                referenceCCtor.Name        = referenceCCtor.Name + "/" + moduleManager.Module.Name;
                                referenceCCtor.Attributes &= ~MethodAttributes.SpecialName;
                                // 2. adding
                                targetModuleModuleType.Methods.Add(referenceCCtor);
                                // 3. calling
                                targetModuleCctor.Body.Instructions.Add(new Instruction(OpCodes.Call, targetModule.Import(referenceCCtor)));
                            }
                        }
                    }
                    else
                    {
                        // other case: simply move the type

                        // check if there is a conflict, and if there is, change new type name
                        var existingType = targetModule.Find(referenceType.FullName, true);
                        if (existingType != null)
                        {
                            referenceType.Name = GetMergedName(referenceType, moduleManager.Module);
                        }
                        // and add the type
                        targetModule.Types.Add(referenceType);
                    }
                }

                // TODO: resources
                // TODO: attributes?

                var relocator = new ModuleRelocator(moduleManager.Module, targetModule);
                relocator.Relocate();
            }
            assemblyFile.DeleteIfLocal();
            return(moduleManager);
        }