List <ProjectFile>?TryCreateResourceFiles(ModuleDef module, ResourceNameCreator resourceNameCreator, EmbeddedResource er) { ResourceElementSet set; try { set = ResourceReader.Read(module, er.CreateReader()); } catch { return(null); } if (IsXamlResource(module, er.Name, set)) { return(CreateXamlResourceFiles(module, resourceNameCreator, set).ToList()); } if (Options.CreateResX) { string filename = resourceNameCreator.GetResxFilename(er.Name, out string typeFullName); return(new List <ProjectFile>() { CreateResXFile(module, er, set, filename, typeFullName, false) }); } return(null); }
IEnumerable <ProjectFile> CreateXamlResourceFiles(ModuleDef module, ResourceNameCreator resourceNameCreator, ResourceElementSet set) { bool decompileBaml = Options.DecompileXaml && !(Options.DecompileBaml is null); foreach (var e in set.ResourceElements) { Debug.Assert(e.ResourceData.Code == ResourceTypeCode.ByteArray || e.ResourceData.Code == ResourceTypeCode.Stream); var data = (byte[])((BuiltInResourceData)e.ResourceData).Data; var rsrcName = Uri.UnescapeDataString(e.Name); if (decompileBaml && rsrcName.EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { var filename = resourceNameCreator.GetBamlResourceName(rsrcName, out string typeFullName); yield return(new BamlResourceProjectFile(filename, data, typeFullName, (bamlData, stream) => Options.DecompileBaml !(module, bamlData, Options.DecompilationContext.CancellationToken, stream))); } else if (StringComparer.InvariantCultureIgnoreCase.Equals(splashScreenImageName, e.Name)) { var filename = resourceNameCreator.GetXamlResourceFilename(rsrcName); yield return(new SplashScreenProjectFile(filename, data, e.Name)); } else { var filename = resourceNameCreator.GetXamlResourceFilename(rsrcName); yield return(new ResourceProjectFile(filename, data, e.Name)); } } }
IEnumerable <ProjectFile> CreateEmbeddedResourceFiles(ModuleDef module, ResourceNameCreator resourceNameCreator, EmbeddedResource er) { if (!Options.UnpackResources) { yield return(CreateRawEmbeddedResourceProjectFile(module, resourceNameCreator, er)); yield break; } er.Data.Position = 0; if (ResourceReader.CouldBeResourcesFile(er.Data)) { var files = TryCreateResourceFiles(module, resourceNameCreator, er); if (files != null) { foreach (var file in files) { yield return(file); } yield break; } } yield return(CreateRawEmbeddedResourceProjectFile(module, resourceNameCreator, er)); }
RawEmbeddedResourceProjectFile CreateRawEmbeddedResourceProjectFile(ModuleDef module, ResourceNameCreator resourceNameCreator, EmbeddedResource er) => new RawEmbeddedResourceProjectFile(resourceNameCreator.GetResourceFilename(er.Name), er);
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)); } } } }
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 != null && ep.DeclaringType != null) StartupObject = ep.DeclaringType.ReflectionFullName; applicationManifest = ApplicationManifest.TryCreate(Options.Module.Win32Resources, filenameCreator); if (ApplicationManifest != 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)).Where(a => a != null), 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)); } } }
IEnumerable<ProjectFile> CreateXamlResourceFiles(ModuleDef module, ResourceNameCreator resourceNameCreator, ResourceElementSet set) { bool decompileBaml = Options.DecompileXaml && Options.DecompileBaml != null; foreach (var e in set.ResourceElements) { Debug.Assert(e.ResourceData.Code == ResourceTypeCode.ByteArray || e.ResourceData.Code == ResourceTypeCode.Stream); var data = (byte[])((BuiltInResourceData)e.ResourceData).Data; if (decompileBaml && e.Name.EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { string typeFullName; var filename = resourceNameCreator.GetBamlResourceName(e.Name, out typeFullName); yield return new BamlResourceProjectFile(filename, data, typeFullName, (bamlData, stream) => Options.DecompileBaml(module, bamlData, Options.DecompilationContext.CancellationToken, stream)); } else if (StringComparer.InvariantCultureIgnoreCase.Equals(splashScreenImageName, e.Name)) { var filename = resourceNameCreator.GetXamlResourceFilename(e.Name); yield return new SplashScreenProjectFile(filename, data, e.Name); } else { var filename = resourceNameCreator.GetXamlResourceFilename(e.Name); yield return new ResourceProjectFile(filename, data, e.Name); } } }
List<ProjectFile> TryCreateResourceFiles(ModuleDef module, ResourceNameCreator resourceNameCreator, EmbeddedResource er) { ResourceElementSet set; try { er.Data.Position = 0; set = ResourceReader.Read(module, er.Data); } catch { return null; } if (IsXamlResource(module, er.Name, set)) return CreateXamlResourceFiles(module, resourceNameCreator, set).ToList(); if (Options.CreateResX) { string typeFullName; string filename = resourceNameCreator.GetResxFilename(er.Name, out typeFullName); return new List<ProjectFile>() { CreateResXFile(module, er, set, filename, typeFullName, false) }; } return null; }
IEnumerable<ProjectFile> CreateEmbeddedResourceFiles(ModuleDef module, ResourceNameCreator resourceNameCreator, EmbeddedResource er) { if (!Options.UnpackResources) { yield return CreateRawEmbeddedResourceProjectFile(module, resourceNameCreator, er); yield break; } er.Data.Position = 0; if (ResourceReader.CouldBeResourcesFile(er.Data)) { var files = TryCreateResourceFiles(module, resourceNameCreator, er); if (files != null) { foreach (var file in files) yield return file; yield break; } } yield return CreateRawEmbeddedResourceProjectFile(module, resourceNameCreator, er); }