Esempio n. 1
0
        void Decompile()
        {
            foreach (var dir in asmPaths)
            {
                AddSearchPath(dir);
            }
            foreach (var dir in userGacPaths)
            {
                AddSearchPath(dir);
            }
            assemblyResolver.UseGAC = useGac;

            var    files      = new List <ProjectModuleOptions>(GetDotNetFiles());
            string guidStr    = projectGuid.ToString();
            int    guidNum    = int.Parse(guidStr.Substring(36 - 8, 8), NumberStyles.HexNumber);
            string guidFormat = guidStr.Substring(0, 36 - 8) + "{0:X8}";

            foreach (var file in files.OrderBy(a => a.Module.Location, StringComparer.InvariantCultureIgnoreCase))
            {
                file.ProjectGuid = new Guid(string.Format(guidFormat, guidNum++));
            }

            if (mdToken != 0 || typeName != null)
            {
                if (files.Count == 0)
                {
                    throw new ErrorException(dnSpy_Console_Resources.MissingDotNetFilename);
                }
                if (files.Count != 1)
                {
                    throw new ErrorException(dnSpy_Console_Resources.OnlyOneFileCanBeDecompiled);
                }

                IMemberDef member;
                if (typeName != null)
                {
                    member = FindType(files[0].Module, typeName);
                }
                else
                {
                    member = files[0].Module.ResolveToken(mdToken) as IMemberDef;
                }
                if (member == null)
                {
                    if (typeName != null)
                    {
                        throw new ErrorException(string.Format(dnSpy_Console_Resources.CouldNotFindTypeX, typeName));
                    }
                    throw new ErrorException(dnSpy_Console_Resources.InvalidToken);
                }

                var writer = Console.Out;
                var output = new PlainTextOutput(writer);

                var lang = GetLanguage();
                if (member is MethodDef)
                {
                    lang.Decompile((MethodDef)member, output, decompilationContext);
                }
                else if (member is FieldDef)
                {
                    lang.Decompile((FieldDef)member, output, decompilationContext);
                }
                else if (member is PropertyDef)
                {
                    lang.Decompile((PropertyDef)member, output, decompilationContext);
                }
                else if (member is EventDef)
                {
                    lang.Decompile((EventDef)member, output, decompilationContext);
                }
                else if (member is TypeDef)
                {
                    lang.Decompile((TypeDef)member, output, decompilationContext);
                }
                else
                {
                    throw new ErrorException(dnSpy_Console_Resources.InvalidMemberToDecompile);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(outputDir))
                {
                    throw new ErrorException(dnSpy_Console_Resources.MissingOutputDir);
                }
                if (GetLanguage().ProjectFileExtension == null)
                {
                    throw new ErrorException(string.Format(dnSpy_Console_Resources.LanguageXDoesNotSupportProjects, GetLanguage().UniqueNameUI));
                }

                var options = new ProjectCreatorOptions(outputDir, decompilationContext.CancellationToken);
                options.Logger          = this;
                options.ProjectVersion  = projectVersion;
                options.NumberOfThreads = numThreads;
                options.ProjectModules.AddRange(files);
                options.UserGACPaths.AddRange(userGacPaths);
                if (createSlnFile && !string.IsNullOrEmpty(slnName))
                {
                    options.SolutionFilename = slnName;
                }
                var creator = new MSBuildProjectCreator(options);
                creator.Create();
            }
        }
Esempio n. 2
0
            public void Execute(ExportToProjectVM vm)
            {
                vm.ProgressMinimum = 0;
                vm.ProgressMaximum = 1;
                vm.TotalProgress   = 0;
                vm.IsIndeterminate = false;
                Task.Factory.StartNew(() => {
                    var decompilationContext = new DecompilationContext {
                        CancellationToken      = cancellationToken,
                        GetDisableAssemblyLoad = () => owner.documentTreeView.DocumentService.DisableAssemblyLoad(),
                    };
                    var options            = new ProjectCreatorOptions(vm.Directory, cancellationToken);
                    options.ProjectVersion = vm.ProjectVersion;
                    if (vm.CreateSolution)
                    {
                        options.SolutionFilename = vm.SolutionFilename;
                    }
                    options.Logger           = this;
                    options.ProgressListener = this;

                    bool hasProjectGuid = vm.ProjectGuid.Value != null;
                    string guidFormat   = null;
                    int guidNum         = 0;
                    if (hasProjectGuid)
                    {
                        string guidStr = vm.ProjectGuid.Value.ToString();
                        guidNum        = int.Parse(guidStr.Substring(36 - 8, 8), NumberStyles.HexNumber);
                        guidFormat     = guidStr.Substring(0, 36 - 8) + "{0:X8}";
                    }
                    foreach (var module in modules.OrderBy(a => a.Location, StringComparer.InvariantCultureIgnoreCase))
                    {
                        var projOpts = new ProjectModuleOptions(module, vm.Decompiler, decompilationContext)
                        {
                            DontReferenceStdLib = vm.DontReferenceStdLib,
                            UnpackResources     = vm.UnpackResources,
                            CreateResX          = vm.CreateResX,
                            DecompileXaml       = vm.DecompileXaml,
                            ProjectGuid         = hasProjectGuid ? new Guid(string.Format(guidFormat, guidNum++)) : Guid.NewGuid(),
                        };
                        if (bamlDecompiler != null)
                        {
                            var o = BamlDecompilerOptions.Create(vm.Decompiler);
                            projOpts.DecompileBaml = (a, b, c, d) => bamlDecompiler.Decompile(a, b, c, o, d);
                        }
                        options.ProjectModules.Add(projOpts);
                    }
                    var creator = new MSBuildProjectCreator(options);

                    creator.Create();
                    if (vm.CreateSolution)
                    {
                        fileToOpen = creator.SolutionFilename;
                    }
                    else
                    {
                        fileToOpen = creator.ProjectFilenames.FirstOrDefault();
                    }
                }, cancellationToken)
                .ContinueWith(t => {
                    var ex = t.Exception;
                    if (ex != null)
                    {
                        Error(string.Format(dnSpy_Resources.ErrorExceptionOccurred, ex));
                    }
                    EmtpyErrorList();
                    vm.OnExportComplete();
                    if (!vm.ExportErrors)
                    {
                        dlg.Close();
                        if (vm.OpenProject)
                        {
                            OpenProject();
                        }
                    }
                }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            }
Esempio n. 3
0
            public void Execute(ExportToProjectVM vm)
            {
                vm.ProgressMinimum = 0;
                vm.ProgressMaximum = 1;
                vm.TotalProgress   = 0;
                vm.IsIndeterminate = false;
                Task.Factory.StartNew(() => {
                    AppCulture.InitializeCulture();
                    var decompilationContext = new DecompilationContext {
                        CancellationToken      = cancellationTokenSource.Token,
                        GetDisableAssemblyLoad = () => owner.fileTreeView.FileManager.DisableAssemblyLoad(),
                    };
                    var options            = new ProjectCreatorOptions(vm.Directory, cancellationTokenSource.Token);
                    options.ProjectVersion = vm.ProjectVersion;
                    if (vm.CreateSolution)
                    {
                        options.SolutionFilename = vm.SolutionFilename;
                    }
                    options.Logger           = this;
                    options.ProgressListener = this;
                    foreach (var module in modules)
                    {
                        var projOpts = new ProjectModuleOptions(module, vm.Language, decompilationContext)
                        {
                            DontReferenceStdLib = vm.DontReferenceStdLib,
                            UnpackResources     = vm.UnpackResources,
                            CreateResX          = vm.CreateResX,
                            DecompileXaml       = vm.DecompileXaml,
                        };
                        if (owner.bamlDecompiler != null)
                        {
                            var o = BamlDecompilerOptions.Create(vm.Language);
                            projOpts.DecompileBaml = (a, b, c, d) => owner.bamlDecompiler.Value.Decompile(a, b, c, o, d);
                        }
                        options.ProjectModules.Add(projOpts);
                    }
                    var creator = new MSBuildProjectCreator(options);

                    creator.Create();
                    if (vm.CreateSolution)
                    {
                        fileToOpen = creator.SolutionFilename;
                    }
                    else
                    {
                        fileToOpen = creator.ProjectFilenames.FirstOrDefault();
                    }
                }, cancellationTokenSource.Token)
                .ContinueWith(t => {
                    var ex = t.Exception;
                    if (ex != null)
                    {
                        Error(string.Format(dnSpy_Resources.ErrorExceptionOccurred, ex));
                    }
                    EmtpyErrorList();
                    vm.OnExportComplete();
                    if (!vm.ExportErrors)
                    {
                        dlg.Close();
                        if (vm.OpenProject)
                        {
                            OpenProject();
                        }
                    }
                }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            }