private void CopyAppConfig(ModuleDefinition module)
        {
            string originalAppConfigFilePath = module.FilePath + ".config";
            string targetAppConfigFilePath   = Path.Combine(this.targetDir, "App.config");

            if (File.Exists(originalAppConfigFilePath))
            {
                bool hasErrors = false;
                if (targetAppConfigFilePath.Length <= MaxPathLength && this.assembly.Modules.Count == 1)
                {
                    try
                    {
                        File.Copy(originalAppConfigFilePath, targetAppConfigFilePath, true);
                    }
                    catch (Exception ex)
                    {
                        hasErrors = true;

                        OnExceptionThrown(ex);
                    }

                    if (!hasErrors)
                    {
                        this.ProjectFileManager.WriteAppConfigFileEntryProjectItem();
                    }
                }
                else
                {
                    hasErrors = true;
                }

                IFileGeneratedInfo appConfigArgs = new FileGeneratedInfo(targetAppConfigFilePath, hasErrors);
                this.OnProjectFileCreated(appConfigArgs);
            }
        }
        private void ProcessXamlResources(EmbeddedResource resource, ModuleDefinition module)
        {
            try
            {
                string targetDir = Path.GetDirectoryName(TargetPath);
                using (ResourceReader resourceReader = new ResourceReader(resource.GetResourceStream()))
                {
                    foreach (System.Collections.DictionaryEntry resourceEntry in resourceReader)
                    {
                        string xamlResourceKey = Utilities.GetXamlResourceKey(resourceEntry, module);

                        bool isBamlResource = ((string)resourceEntry.Key).EndsWith(".baml", StringComparison.OrdinalIgnoreCase);

                        string xamlResourceRelativePath = xamlResourcesToPathsMap[xamlResourceKey];
                        string fullPath = Path.Combine(targetDir, xamlResourceRelativePath);

                        string fullClassName = TryWriteBamlResource(fullPath, isBamlResource, resourceEntry.Value as UnmanagedMemoryStream);
                        if (fullClassName != null)
                        {
                            this.projectFileManager.XamlFullNameToRelativePathMap.Add(fullClassName, xamlResourceRelativePath);
                        }
                        else
                        {
                            this.projectFileManager.AddResourceToOtherXamlResources(xamlResourceRelativePath);
                        }

                        IFileGeneratedInfo args = new FileGeneratedInfo(fullPath, false);
                        OnProjectFileCreated(args);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ResourceWritingFailure != null)
                {
                    ResourceWritingFailure(this, resource.Name, ex);
                }

                if (this.projectNotifier != null)
                {
                    this.projectNotifier.OnResourceWritingFailure(resource.Name, ex);
                }
            }
        }
        protected void InformProjectFileCreated(ModuleDefinition module, string extension, bool hasErrors)
        {
            string projFileName;

            if (!modulesToProjectsFilePathsMap.TryGetValue(module, out projFileName))
            {
                throw new Exception("Module project file path not found in modules projects filepaths map.");
            }

            if (!projFileName.EndsWith(extension))
            {
                projFileName += extension;
            }

            string             fullFilePath = Path.Combine(this.targetDir, projFileName);
            IFileGeneratedInfo csprojArgs   = new FileGeneratedInfo(fullFilePath, hasErrors);

            this.OnProjectFileCreated(csprojArgs);
        }
        protected void CreateResources(ModuleDefinition module)
        {
            string targetDir = Path.GetDirectoryName(this.TargetPath);

            foreach (Resource resource in module.Resources)
            {
                try
                {
                    if (!this.decompilationPreferences.DecompileDangerousResources &&
                        DangerousResourceIdentifier.IsDangerousResource(resource))
                    {
                        continue;
                    }

                    if (resource.ResourceType != ResourceType.Embedded)
                    {
                        continue;
                    }

                    EmbeddedResource   embeddedResource = (EmbeddedResource)resource;
                    IFileGeneratedInfo args;
                    if (resource.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!embeddedResource.Name.EndsWith(".g.resources", StringComparison.OrdinalIgnoreCase))
                        {
                            string resourceName         = embeddedResource.Name.Substring(0, embeddedResource.Name.Length - 10); //".resources".Length == 10
                            string relativeResourcePath = resourcesToPathsMap[resource];
                            string fullResourcePath     = Path.Combine(targetDir, relativeResourcePath);

                            if (TryCreateResXFile(embeddedResource, fullResourcePath))
                            {
                                this.projectFileManager.ResourceDesignerMap.Add(resourceName, relativeResourcePath);
                                args = new FileGeneratedInfo(fullResourcePath, false);
                                OnProjectFileCreated(args);
                            }
                        }
                        else
                        {
                            ProcessXamlResources(embeddedResource, module);
                        }
                    }
                    else
                    {
                        string resourceLegalName = resourcesToPathsMap[resource];
                        string resourceFullPath  = Path.Combine(targetDir, resourceLegalName);
                        using (FileStream fileStream = new FileStream(resourceFullPath, FileMode.Create, FileAccess.Write))
                        {
                            embeddedResource.GetResourceStream().CopyTo(fileStream);
                        }

                        this.projectFileManager.AddResourceToOtherEmbeddedResources(resourceLegalName);

                        args = new FileGeneratedInfo(resourceFullPath, false);
                        OnProjectFileCreated(args);
                    }
                }
                catch (Exception ex)
                {
                    if (ResourceWritingFailure != null)
                    {
                        ResourceWritingFailure(this, resource.Name, ex);
                    }

                    if (this.projectNotifier != null)
                    {
                        this.projectNotifier.OnResourceWritingFailure(resource.Name, ex);
                    }
                }
            }
        }
        private bool BuildProjectInternal(Func <bool> shouldCancel)
        {
            if (this.fileGeneratedNotifier != null)
            {
                this.fileGeneratedNotifier.TotalFileCount = this.NumberOfFilesToGenerate;
            }

            try
            {
                foreach (ModuleDefinition module in assembly.Modules)
                {
                    ModuleDefinition currentModule = module;

                    CreateResources(module);
#if !NET35
                    Task.Factory.StartNew(
                        () => WriteUserDefinedTypes(currentModule, shouldCancel))
                    .Wait();
#else
                    WriteUserDefinedTypes(currentModule, shouldCancel);
#endif

                    if (shouldCancel())
                    {
                        return(true);
                    }

                    bool isMainModule = Utilities.IsMainModule(module);

                    if (isMainModule)
                    {
                        bool   createdFile;
                        string assemblyInfoRelativePath = WriteAssemblyInfo(assembly, out createdFile);
                        if (createdFile)
                        {
                            this.ProjectFileManager.IncludeAssemblyInfo(assemblyInfoRelativePath);

                            IFileGeneratedInfo assemblyInfoArgs = new FileGeneratedInfo(Path.Combine(this.targetDir, assemblyInfoRelativePath), false);
                            this.OnProjectFileCreated(assemblyInfoArgs);
                        }
                    }

                    CopyAppConfig(module);

                    if (this.projectGenerationSettings != null && !this.projectGenerationSettings.JustDecompileSupportedProjectType && module.IsMain)
                    {
                        StreamWriter writer;
                        if (this.TargetPath.EndsWith(language.VSProjectFileExtension + ErrorFileExtension))
                        {
                            writer = new StreamWriter(this.TargetPath);
                        }
                        else
                        {
                            writer = new StreamWriter(this.TargetPath + language.VSProjectFileExtension + ErrorFileExtension);
                        }

                        using (writer)
                        {
                            writer.Write("JustDecompile: " + this.projectGenerationSettings.ErrorMessage);
                        }

                        InformProjectFileCreated(module, language.VSProjectFileExtension + ErrorFileExtension, false);
                    }
                    else
                    {
                        bool exceptionsWhenCreatingProjectFile = false;
                        bool projectFileCreated = false;
                        try
                        {
                            if (isMainModule)
                            {
                                projectFileCreated = this.WriteMainModuleProjectFile(module);
                            }
                            else
                            {
                                projectFileCreated = this.WriteNetModuleProjectFile(module);
                            }
                        }
                        catch (Exception ex)
                        {
                            exceptionsWhenCreatingProjectFile = true;

                            OnExceptionThrown(ex);
                        }

                        if (projectFileCreated)
                        {
                            InformProjectFileCreated(module, this.language.VSProjectFileExtension, exceptionsWhenCreatingProjectFile);
                        }
                    }

                    WriteModuleAdditionalFiles(module);
                }

                if (this.projectGenerationSettings == null || this.projectGenerationSettings.JustDecompileSupportedProjectType)
                {
                    // Write the solution file
                    bool exceptionWhileWritingSolutionFile = false;
                    bool solutionFileCreated = false;
                    try
                    {
                        solutionFileCreated = WriteSolutionFile();
                    }
                    catch (Exception ex)
                    {
                        exceptionWhileWritingSolutionFile = true;

                        OnExceptionThrown(ex);
                    }

                    if (solutionFileCreated)
                    {
                        string             solutionFilePath = Path.Combine(this.targetDir, this.filePathsService.GetSolutionRelativePath());
                        IFileGeneratedInfo solutionArgs     = new FileGeneratedInfo(solutionFilePath, exceptionWhileWritingSolutionFile);
                        this.OnProjectFileCreated(solutionArgs);
                    }
                }
            }
            catch (Exception ex)
            {
                base.OnProjectGenerationFailure(ex);

                if (this.projectNotifier != null)
                {
                    this.projectNotifier.OnProjectGenerationFailure(ex);
                }
            }
            finally
            {
                OnProjectGenerationFinished();
            }
            if (decompilationPreferences.WriteDocumentation)
            {
                /// Clear the cached documentation
                Telerik.JustDecompiler.XmlDocumentationReaders.DocumentationManager.ClearCache();
            }

            ClearCaches();

            return(false);
        }
 public void OnProjectFileGenerated(Telerik.JustDecompiler.External.Interfaces.IFileGeneratedInfo args)
 {
     FileGeneratedInfo fileGeneratedInfo = new FileGeneratedInfo(args.FullPath, args.HasErrors);
     this.notifier.OnProjectFileGenerated(fileGeneratedInfo);
 }