예제 #1
0
        static void Main()
        {
            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

            var assemblyPath = Path.Combine(baseDirectory, "ShellAssembly.exe");
            var module       = ModuleHelper.LoadModule(assemblyPath);

            var codeImporter = new CodeImporter(module)
            {
                HideImportedTypes = false,
                ModuleResolver    = new AssemblyModuleResolver(
                    typeof(Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialog).Assembly,
                    typeof(Microsoft.WindowsAPICodePack.Dialogs.TaskDialog).Assembly,
                    typeof(Newtonsoft.Json.JsonConvert).Assembly)
            };

            codeImporter.ILMerge();

            var tempPath = TestHelper.TempPath;

            var importedModules = codeImporter.ListImportedModules();

            foreach (var m in importedModules)
            {
                foreach (var resource in m.Resources.OfType <EmbeddedResource>())
                {
                    module.Resources.Add(resource);
                }
            }

            var targetAssemblyPath = Path.Combine(tempPath, "ShellAssembly2.exe");

            module.Assembly.Name.Name = "ShellAssembly2";
            var now = DateTime.Now;

            module.Assembly.Name.Version = new Version(now.Year, now.Month, now.Day, (int)now.TimeOfDay.TotalMilliseconds);
            module.Write(targetAssemblyPath);

            var peVerify = TestHelper.PEVerify.Verify(targetAssemblyPath, line => Console.WriteLine(line));

            Assert.True(peVerify);

            var importedTypes = codeImporter.ListImportedTypes();

            TestHelper.VerifyTypes(importedTypes, importedModules, targetAssemblyPath, AssertIl);

            var il = TestHelper.ILDasm.Decompile(targetAssemblyPath);

            File.WriteAllText(Path.ChangeExtension(targetAssemblyPath, ".il"), il);

            Console.WriteLine("Done - press any key...");
            Console.ReadKey();
        }
예제 #2
0
        public override void Execute()
        {
#if DEBUG && LAUNCH_DEBUGGER
            System.Diagnostics.Debugger.Launch();
#endif

            var includeAssemblies = BuildRegex(ReadConfigValue("IncludeAssemblies", string.Empty));
            var excludeAssemblies = BuildRegex(ReadConfigValue("ExcludeAssemblies", string.Empty));
            var includeResources  = BuildRegex(ReadConfigValue("IncludeResources", string.Empty));
            var excludeResources  = BuildRegex(ReadConfigValue("ExcludeResources", string.Empty));
            var hideImportedTypes = ReadConfigValue("HideImportedTypes", true);
            var namespacePrefix   = ReadConfigValue("NamespacePrefix", string.Empty);
            var compactMode       = ReadConfigValue("CompactMode", false);
            var fullImport        = ReadConfigValue("FullImport", false);

            var isDotNetCore = ModuleDefinition.IsTargetFrameworkDotNetCore();

            var references = isDotNetCore ? (IList <string>)References.Split(';') : ReferenceCopyLocalPaths;

            var codeImporter = new CodeImporter(ModuleDefinition)
            {
                ModuleResolver     = new LocalReferenceModuleResolver(this, references, includeAssemblies, excludeAssemblies),
                HideImportedTypes  = hideImportedTypes,
                NamespaceDecorator = name => namespacePrefix + name,
                CompactMode        = compactMode && !fullImport,
            };

            codeImporter.ILMerge();

            var importedModules = codeImporter.ListImportedModules();

            if (fullImport)
            {
                importedModules = ImportRemainingTypes(importedModules, codeImporter);
            }

            ValidateBamlReferences(ModuleDefinition, importedModules);

            ImportResources(ModuleDefinition, importedModules, includeResources, excludeResources, this);

            var importedReferences = new HashSet <string>(importedModules.Select(moduleDefinition => Path.GetFileNameWithoutExtension(moduleDefinition.FileName)), StringComparer.OrdinalIgnoreCase);

            bool IsImportedReference(string path)
            {
                return(importedReferences.Contains(Path.GetFileNameWithoutExtension(path)));
            }

            ReferenceCopyLocalPaths.RemoveAll(IsImportedReference);
            RuntimeCopyLocalPaths.RemoveAll(IsImportedReference);
        }
예제 #3
0
        private static void Execute([NotNull] ModuleDefinition moduleDefinition, [NotNull] ILogger logger, [NotNull] string addInDirectoryPath, [NotNull] string assemblyFilePath, [NotNull] IList <string> referenceCopyLocalPaths)
        {
            var entryPoint = moduleDefinition.EntryPoint;

            if (entryPoint == null)
            {
                logger.LogError("No entry point found in target module.");
                return;
            }

            TypeDefinition splashScreenControl;

            try
            {
                splashScreenControl = moduleDefinition.Types.Single(HasSplashScreenAttribute);
            }
            catch (Exception ex)
            {
                logger.LogError("No single class with the [SplashScreen] attribute found: " + ex.Message);
                return;
            }

            byte[] bitmapData;

            try
            {
                bitmapData = BitmapGenerator.Generate(addInDirectoryPath, assemblyFilePath, splashScreenControl.FullName, referenceCopyLocalPaths);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return;
            }

            var splashScreenControlBamlResourceName = splashScreenControl.Name.ToLowerInvariant() + ".baml";

            ResourceHelper.UpdateResources(moduleDefinition, SplashResourceName, bitmapData, splashScreenControlBamlResourceName);

            moduleDefinition.Types.Remove(splashScreenControl);

            var attribute = GetSplashScreenAttribute(splashScreenControl);

            var minimumVisibilityDuration = attribute.GetPropertyValue(MinimumVisibilityDurationPropertyName, 4.0);
            var fadeoutDuration           = attribute.GetPropertyValue(FadeoutDurationPropertyName, 1.0);

            var referencedModule = attribute.AttributeType.Resolve().Module;

            var adapterType = referencedModule.Types.Single(type => type.Name == SplashScreenAdapterTypeName);

            var importer = new CodeImporter(moduleDefinition);

            adapterType = importer.Import(adapterType);

            importer.ILMerge();

            var adapterTypeConstructor = adapterType.GetConstructors().Single(ctor => ctor.Parameters.Count == 3);

            entryPoint.Body.Instructions.InsertRange(0,
                                                     Instruction.Create(OpCodes.Ldstr, SplashResourceName),
                                                     Instruction.Create(OpCodes.Ldc_R8, minimumVisibilityDuration),
                                                     Instruction.Create(OpCodes.Ldc_R8, fadeoutDuration),
                                                     Instruction.Create(OpCodes.Newobj, moduleDefinition.ImportReference(adapterTypeConstructor)),
                                                     Instruction.Create(OpCodes.Pop)
                                                     );
        }