Пример #1
0
        ProjectFile CreateTypeProjectFile(TypeDef type, FilenameCreator filenameCreator)
        {
            var bamlFile = TryGetBamlFile(type);

            if (!(bamlFile is null))
            {
                var             filename = filenameCreator.Create(GetTypeExtension(type), type.FullName);
                TypeProjectFile newFile;
                var             isAppType = DotNetUtils.IsSystemWindowsApplication(type);
                if (!Options.Decompiler.CanDecompile(DecompilationType.PartialType))
                {
                    newFile = new TypeProjectFile(type, filename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput);
                }
                else
                {
                    newFile = new XamlTypeProjectFile(type, filename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput);
                }
                newFile.DependentUpon = bamlFile;
                if (isAppType && DotNetUtils.IsStartUpClass(type))
                {
                    bamlFile.IsAppDef = true;
                    StartupObject     = null;
                }
                if (isAppType)
                {
                    appTypeProjFile = newFile;
                }
                return(newFile);
            }

            const string DESIGNER = ".Designer";
            var          resxFile = TryGetResXFile(type);

            if (DotNetUtils.IsWinForm(type))
            {
                var fname    = !(resxFile is null) ? Path.GetFileNameWithoutExtension(resxFile.Filename) : type.Name.String;
                var filename = filenameCreator.CreateFromNamespaceName(GetTypeExtension(type), type.ReflectionNamespace, fname);
                var dname    = filenameCreator.CreateFromNamespaceName(GetTypeExtension(type), type.ReflectionNamespace, fname + DESIGNER);

                var newFile = new WinFormsProjectFile(type, filename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput);
                if (!(resxFile is null))
                {
                    resxFile.DependentUpon = newFile;
                }
                var winFormsDesignerFile = new WinFormsDesignerProjectFile(newFile, dname, createDecompilerOutput);
                winFormsDesignerFile.DependentUpon = newFile;
                Files.Add(winFormsDesignerFile);
                return(newFile);
            }
            else if (!(resxFile is null))
            {
                var filename = filenameCreator.CreateFromNamespaceName(GetTypeExtension(type), type.ReflectionNamespace, Path.GetFileNameWithoutExtension(resxFile.Filename) + DESIGNER);
                var newFile  = new TypeProjectFile(type, filename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput);
                newFile.DependentUpon  = resxFile;
                newFile.AutoGen        = true;
                newFile.DesignTime     = true;
                resxFile.Generator     = type.IsPublic ? "PublicResXFileCodeGenerator" : "ResXFileCodeGenerator";
                resxFile.LastGenOutput = newFile;
                return(newFile);
            }

            var bt = type.BaseType;

            if (!(bt is null) && bt.FullName == "System.Configuration.ApplicationSettingsBase")
            {
                var         designerFilename = filenameCreator.Create(DESIGNER + GetTypeExtension(type), type.FullName);
                var         settingsFilename = filenameCreator.Create(".settings", type.FullName);
                ProjectFile designerTypeFile;
                if (Options.Decompiler.CanDecompile(DecompilationType.PartialType))
                {
                    var typeFilename     = filenameCreator.Create(GetTypeExtension(type), type.FullName);
                    var settingsTypeFile = new SettingsTypeProjectFile(type, typeFilename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput);
                    designerTypeFile = new SettingsDesignerTypeProjectFile(settingsTypeFile, designerFilename, createDecompilerOutput);
                    Files.Add(settingsTypeFile);
                }
                else
                {
                    designerTypeFile = new TypeProjectFile(type, designerFilename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput);
                }
                var settingsFile = new SettingsProjectFile(type, settingsFilename);
                designerTypeFile.DependentUpon         = settingsFile;
                designerTypeFile.AutoGen               = true;
                designerTypeFile.DesignTimeSharedInput = true;
                settingsFile.Generator     = type.IsPublic ? "PublicSettingsSingleFileGenerator" : "SettingsSingleFileGenerator";
                settingsFile.LastGenOutput = designerTypeFile;
                Files.Add(settingsFile);
                return(designerTypeFile);
            }

            var newFilename = filenameCreator.Create(GetTypeExtension(type), type.FullName);

            return(new TypeProjectFile(type, newFilename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput));
        }
Пример #2
0
        public void CreateProjectFiles(DecompileContext ctx)
        {
            var filenameCreator     = new FilenameCreator(Directory, DefaultNamespace);
            var resourceNameCreator = new ResourceNameCreator(Options.Module, filenameCreator);

            AllowUnsafeBlocks = DotNetUtils.IsUnsafe(Options.Module);
            InitializeSplashScreen();
            if (Options.Decompiler.CanDecompile(DecompilationType.AssemblyInfo))
            {
                var filename = filenameCreator.CreateFromRelativePath(Path.Combine(PropertiesFolder, "AssemblyInfo"), Options.Decompiler.FileExtension);
                Files.Add(new AssemblyInfoProjectFile(Options.Module, filename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput));
            }

            var ep = Options.Module.EntryPoint;

            if (!(ep is null) && !(ep.DeclaringType is null))
            {
                StartupObject = ep.DeclaringType.ReflectionFullName;
            }

            applicationManifest = ApplicationManifest.TryCreate(Options.Module.Win32Resources, filenameCreator);
            if (!(ApplicationManifest is null))
            {
                Files.Add(new ApplicationManifestProjectFile(ApplicationManifest.Filename));
            }

            foreach (var rsrc in Options.Module.Resources)
            {
                ctx.CancellationToken.ThrowIfCancellationRequested();
                switch (rsrc.ResourceType)
                {
                case ResourceType.Embedded:
                    foreach (var file in CreateEmbeddedResourceFiles(Options.Module, resourceNameCreator, (EmbeddedResource)rsrc))
                    {
                        Files.Add(file);
                        Files.AddRange(CreateSatelliteFiles(rsrc.Name, filenameCreator, file));
                    }
                    break;

                case ResourceType.AssemblyLinked:
                    //TODO: What should be created here?
                    break;

                case ResourceType.Linked:
                    //TODO: What should be created here?
                    break;

                default:
                    break;
                }
            }
            InitializeXaml();
            InitializeResX();
            foreach (var type in Options.Module.Types)
            {
                ctx.CancellationToken.ThrowIfCancellationRequested();
                if (!DecompileType(type))
                {
                    continue;
                }
                Files.Add(CreateTypeProjectFile(type, filenameCreator));
            }
            CreateEmptyAppXamlFile();

            var existingAppConfig = Options.Module.Location + ".config";

            if (File.Exists(existingAppConfig))
            {
                Files.Add(new AppConfigProjectFile(filenameCreator.CreateName("app.config"), existingAppConfig));
            }

            applicationIcon = ApplicationIcon.TryCreate(Options.Module.Win32Resources, Path.GetFileName(Directory), filenameCreator);

            var dirs   = new HashSet <string>(Files.Select(a => GetDirectoryName(a.Filename)).OfType <string>(), StringComparer.OrdinalIgnoreCase);
            int errors = 0;

            foreach (var dir in dirs)
            {
                ctx.CancellationToken.ThrowIfCancellationRequested();
                try {
                    System.IO.Directory.CreateDirectory(dir);
                }
                catch (Exception ex) {
                    if (errors++ < 20)
                    {
                        ctx.Logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_CouldNotCreateDirectory2, dir, ex.Message));
                    }
                }
            }
        }