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)
                    {
                        hasErrors = true;
                    }

                    if (!hasErrors)
                    {
                        ProjectItemGroupNone appConfigFileEntry = new ProjectItemGroupNone() { Include = "App.config" };
                        this.fileGenContext.AppConfigFileEntry = appConfigFileEntry;
                    }
                }
                else
                {
                    hasErrors = true;
                }

                IFileGeneratedInfo appConfigArgs = new FileGeneratedInfo(targetAppConfigFilePath, hasErrors);
                this.OnProjectFileCreated(appConfigArgs);
            }
        }
        private 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);
        }
        private void ProcessXamlResources(EmbeddedResource resource, ModuleDefinition module)
        {
            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)
                    {
                        fileGenContext.XamlFullNameToRelativePathMap.Add(fullClassName, xamlResourceRelativePath);
                    }
                    else
                    {
                        fileGenContext.OtherXamlResources.Add(new ProjectItemGroupResource() { Include = xamlResourceRelativePath });
                    }

					IFileGeneratedInfo args = new FileGeneratedInfo(fullPath, false);
                    OnProjectFileCreated(args);
                }
            }
        }
        private bool BuildProjectInternal(Func<bool> shouldCancel)
        {

			if (this.fileGeneratedNotifier != null)
			{
				this.fileGeneratedNotifier.TotalFileCount = this.NumberOfFilesToGenerate;
			}

			this.modulesProjectsGuids = new Dictionary<ModuleDefinition, Guid>();

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

					fileGenContext = new FileGenerationContext(this.targetDir, namespaceHierarchyTree);

					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)
						{
							ProjectItemGroupCompile assemblyInfoFileEntry = new ProjectItemGroupCompile();
							assemblyInfoFileEntry.Include = assemblyInfoRelativePath;
							fileGenContext.AssemblyInfoFileEntry = assemblyInfoFileEntry;
							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 = WriteMainModuleProjectFile(module);
                            }
                            else
                            {
								projectFileCreated = WriteNetModuleProjectFile(module);
                            }
                        }
                        catch (Exception)
                        {
                            exceptionsWhenCreatingProjectFile = true;
                        }

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

                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;
                    }

					if (solutionFileCreated)
					{
						string solutionFilePath = Path.Combine(this.targetDir, this.filePathsService.GetSolutionRelativePath());
						IFileGeneratedInfo solutionArgs = new FileGeneratedInfo(solutionFilePath, exceptionWhileWritingSolutionFile);
						this.OnProjectFileCreated(solutionArgs);
					}
                }
            }
            catch (Exception ex)
            {
                if (ProjectGenerationFailure != null)
                {
                    ProjectGenerationFailure(this, 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;
        }
        private void CreateResources(ModuleDefinition module)
        {
			string targetDir = Path.GetDirectoryName(this.TargetPath);
			foreach (Resource resource in module.Resources)
			{
				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))
						{
							fileGenContext.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);
					}

					fileGenContext.OtherEmbeddedResources.Add(new ProjectItemGroupEmbeddedResource() { Include = resourceLegalName });

					args = new FileGeneratedInfo(resourceFullPath, false);
					OnProjectFileCreated(args);
				}
			}
        }