Exemplo n.º 1
0
        public string GetResxFilename(string resourceName, out string typeFullName)
        {
            const string RESOURCES_EXT = ".resources";
            const string RESX_EXT      = ".resx";
            var          n             = resourceName;

            if (n.EndsWith(RESOURCES_EXT, StringComparison.OrdinalIgnoreCase))
            {
                n = n.Substring(0, n.Length - RESOURCES_EXT.Length);
            }

            var type = module.Find(n, true);

            if (!(type is null) && DotNetUtils.IsWinForm(type))
            {
                typeFullName = type.ReflectionFullName;
                return(filenameCreator.CreateFromNamespaceName(RESX_EXT, type.Namespace, type.Name));
            }

            var resXType = GetResXType(type, n);

            if (!(resXType is null))
            {
                typeFullName = resXType.ReflectionFullName;
                return(filenameCreator.CreateFromNamespaceName(RESX_EXT, resXType.ReflectionNamespace, GetResxDesignerFilename(resXType.ReflectionNamespace, n)));
            }

            typeFullName = n;
            return(filenameCreator.Create(RESX_EXT, n));
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        public void Create()
        {
            SatelliteAssemblyFinder?satelliteAssemblyFinder = null;

            try {
                var opts = new ParallelOptions {
                    CancellationToken      = options.CancellationToken,
                    MaxDegreeOfParallelism = options.NumberOfThreads <= 0 ? Environment.ProcessorCount : options.NumberOfThreads,
                };
                var filenameCreator = new FilenameCreator(options.Directory);
                var ctx             = new DecompileContext(options.CancellationToken, logger);
                satelliteAssemblyFinder = new SatelliteAssemblyFinder();
                Parallel.ForEach(options.ProjectModules, opts, modOpts => {
                    options.CancellationToken.ThrowIfCancellationRequested();
                    string name;
                    lock (filenameCreator)
                        name = filenameCreator.Create(modOpts.Module);
                    var p = new Project(modOpts, name, satelliteAssemblyFinder, options.CreateDecompilerOutput);
                    lock (projects)
                        projects.Add(p);
                    p.CreateProjectFiles(ctx);
                });

                var  jobs = GetJobs().ToArray();
                bool writeSolutionFile = !string.IsNullOrEmpty(options.SolutionFilename);
                int  maxProgress       = jobs.Length + projects.Count;
                if (writeSolutionFile)
                {
                    maxProgress++;
                }
                progressListener.SetMaxProgress(maxProgress);

                Parallel.ForEach(GetJobs(), opts, job => {
                    options.CancellationToken.ThrowIfCancellationRequested();
                    try {
                        job.Create(ctx);
                    }
                    catch (OperationCanceledException) {
                        throw;
                    }
                    catch (Exception ex) {
                        if (job is IFileJob fjob)
                        {
                            logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FileCreationFailed3, fjob.Filename, job.Description, ex.Message));
                        }
                        else
                        {
                            logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FileCreationFailed2, job.Description, ex.Message));
                        }
                    }
                    progressListener.SetProgress(Interlocked.Increment(ref totalProgress));
                });
                Parallel.ForEach(projects, opts, p => {
                    options.CancellationToken.ThrowIfCancellationRequested();
                    try {
                        var writer = new ProjectWriter(p, p.Options.ProjectVersion ?? options.ProjectVersion, projects, options.UserGACPaths);
                        writer.Write();
                    }
                    catch (OperationCanceledException) {
                        throw;
                    }
                    catch (Exception ex) {
                        logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FailedToCreateProjectFile, p.Filename, ex.Message));
                    }
                    progressListener.SetProgress(Interlocked.Increment(ref totalProgress));
                });
                if (writeSolutionFile)
                {
                    options.CancellationToken.ThrowIfCancellationRequested();
                    try {
                        var writer = new SolutionWriter(options.ProjectVersion, projects, SolutionFilename);
                        writer.Write();
                    }
                    catch (OperationCanceledException) {
                        throw;
                    }
                    catch (Exception ex) {
                        logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FailedToCreateSolutionFile, SolutionFilename, ex.Message));
                    }
                    progressListener.SetProgress(Interlocked.Increment(ref totalProgress));
                }
                Debug.Assert(totalProgress == maxProgress);
                progressListener.SetProgress(maxProgress);
            }
            finally {
                if (satelliteAssemblyFinder is not null)
                {
                    satelliteAssemblyFinder.Dispose();
                }
            }
        }
Exemplo n.º 4
0
		ProjectFile CreateTypeProjectFile(TypeDef type, FilenameCreator filenameCreator) {
			var bamlFile = TryGetBamlFile(type);
			if (bamlFile != 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 (resxFile != null) {
				if (DotNetUtils.IsWinForm(type)) {
					var filename = filenameCreator.CreateFromNamespaceName(GetTypeExtension(type), type.ReflectionNamespace, Path.GetFileNameWithoutExtension(resxFile.Filename));
					var newFile = new WinFormsProjectFile(type, filename, Options.DecompilationContext, Options.Decompiler, createDecompilerOutput);
					resxFile.DependentUpon = newFile;
					var dname = filenameCreator.CreateFromNamespaceName(GetTypeExtension(type), type.ReflectionNamespace, Path.GetFileNameWithoutExtension(resxFile.Filename) + DESIGNER);
					var winFormsDesignerFile = new WinFormsDesignerProjectFile(newFile, dname, createDecompilerOutput);
					winFormsDesignerFile.DependentUpon = newFile;
					Files.Add(winFormsDesignerFile);
					return newFile;
				}
				else {
					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 != 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);
		}
Exemplo n.º 5
0
		public void Create() {
			SatelliteAssemblyFinder satelliteAssemblyFinder = null;
			try {
				var opts = new ParallelOptions {
					CancellationToken = options.CancellationToken,
					MaxDegreeOfParallelism = options.NumberOfThreads <= 0 ? Environment.ProcessorCount : options.NumberOfThreads,
				};
				var filenameCreator = new FilenameCreator(options.Directory);
				var ctx = new DecompileContext(options.CancellationToken, logger);
				satelliteAssemblyFinder = new SatelliteAssemblyFinder();
				Parallel.ForEach(options.ProjectModules, opts, modOpts => {
					options.CancellationToken.ThrowIfCancellationRequested();
					string name;
					lock (filenameCreator)
						name = filenameCreator.Create(modOpts.Module);
					var p = new Project(modOpts, name, satelliteAssemblyFinder, options.CreateDecompilerOutput);
					lock (projects)
						projects.Add(p);
					p.CreateProjectFiles(ctx);
				});

				var jobs = GetJobs().ToArray();
				bool writeSolutionFile = !string.IsNullOrEmpty(options.SolutionFilename);
				int maxProgress = jobs.Length + projects.Count;
				if (writeSolutionFile)
					maxProgress++;
				progressListener.SetMaxProgress(maxProgress);

				Parallel.ForEach(GetJobs(), opts, job => {
					options.CancellationToken.ThrowIfCancellationRequested();
					try {
						job.Create(ctx);
					}
					catch (OperationCanceledException) {
						throw;
					}
					catch (Exception ex) {
						var fjob = job as IFileJob;
						if (fjob != null)
							logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FileCreationFailed3, fjob.Filename, job.Description, ex.Message));
						else
							logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FileCreationFailed2, job.Description, ex.Message));
					}
					progressListener.SetProgress(Interlocked.Increment(ref totalProgress));
				});
				Parallel.ForEach(projects, opts, p => {
					options.CancellationToken.ThrowIfCancellationRequested();
					try {
						var writer = new ProjectWriter(p, p.Options.ProjectVersion ?? options.ProjectVersion, projects, options.UserGACPaths);
						writer.Write();
					}
					catch (OperationCanceledException) {
						throw;
					}
					catch (Exception ex) {
						logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FailedToCreateProjectFile, p.Filename, ex.Message));
					}
					progressListener.SetProgress(Interlocked.Increment(ref totalProgress));
				});
				if (writeSolutionFile) {
					options.CancellationToken.ThrowIfCancellationRequested();
					try {
						var writer = new SolutionWriter(options.ProjectVersion, projects, SolutionFilename);
						writer.Write();
					}
					catch (OperationCanceledException) {
						throw;
					}
					catch (Exception ex) {
						logger.Error(string.Format(dnSpy_Decompiler_Resources.MSBuild_FailedToCreateSolutionFile, SolutionFilename, ex.Message));
					}
					progressListener.SetProgress(Interlocked.Increment(ref totalProgress));
				}
				Debug.Assert(totalProgress == maxProgress);
				progressListener.SetProgress(maxProgress);
			}
			finally {
				if (satelliteAssemblyFinder != null)
					satelliteAssemblyFinder.Dispose();
			}
		}