Skip to content

Commit

Permalink
Fix handling of file extensions in project export.
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Feb 21, 2025
1 parent 292c21d commit 31bbcf4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
Expand Down Expand Up @@ -135,7 +136,7 @@ protected WholeProjectDecompiler(

public void DecompileProject(MetadataFile file, string targetDirectory, CancellationToken cancellationToken = default(CancellationToken))
{
string projectFileName = Path.Combine(targetDirectory, CleanUpFileName(file.Name) + ".csproj");
string projectFileName = Path.Combine(targetDirectory, CleanUpFileName(file.Name, ".csproj"));
using (var writer = new StreamWriter(projectFileName))
{
DecompileProject(file, targetDirectory, writer, cancellationToken);
Expand Down Expand Up @@ -238,7 +239,7 @@ IEnumerable<ProjectItemInfo> WriteCodeFilesInProject(MetadataFile module, IList<
string GetFileFileNameForHandle(TypeDefinitionHandle h)
{
var type = metadata.GetTypeDefinition(h);
string file = CleanUpFileName(metadata.GetString(type.Name) + ".cs");
string file = CleanUpFileName(metadata.GetString(type.Name), ".cs");
string ns = metadata.GetString(type.Namespace);
if (string.IsNullOrEmpty(ns))
{
Expand Down Expand Up @@ -339,8 +340,7 @@ protected virtual IEnumerable<ProjectItemInfo> WriteResourceFilesInProject(Metad
{
foreach (var (name, value) in resourcesFile)
{
string fileName = SanitizeFileName(name)
.Replace('/', Path.DirectorySeparatorChar);
string fileName = SanitizeFileName(name);
string dirName = Path.GetDirectoryName(fileName);
if (!string.IsNullOrEmpty(dirName) && directories.Add(dirName))
{
Expand Down Expand Up @@ -609,9 +609,14 @@ static string CleanUpApplicationManifest(byte[] appManifest)
/// <summary>
/// Cleans up a node name for use as a file name.
/// </summary>
public static string CleanUpFileName(string text)
public static string CleanUpFileName(string text, string extension)
{
return CleanUpName(text, separateAtDots: false, treatAsFileName: false);
Debug.Assert(!string.IsNullOrEmpty(extension));
if (!extension.StartsWith("."))
extension = "." + extension;
text = text + extension;

return CleanUpName(text, separateAtDots: false, treatAsFileName: !string.IsNullOrEmpty(extension), treatAsPath: false);
}

/// <summary>
Expand All @@ -620,7 +625,7 @@ public static string CleanUpFileName(string text)
/// </summary>
public static string SanitizeFileName(string fileName)
{
return CleanUpName(fileName, separateAtDots: false, treatAsFileName: true);
return CleanUpName(fileName, separateAtDots: false, treatAsFileName: true, treatAsPath: true);
}

/// <summary>
Expand All @@ -629,7 +634,7 @@ public static string SanitizeFileName(string fileName)
/// If <paramref name="treatAsFileName"/> is active, we check for file a extension and try to preserve it,
/// if it's valid.
/// </summary>
static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName)
static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName, bool treatAsPath)
{
// Remove anything that could be confused with a rooted path.
int pos = text.IndexOf(':');
Expand Down Expand Up @@ -692,7 +697,7 @@ static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName
if (separateAtDots)
currentSegmentLength = 0;
}
else if (treatAsFileName && (c is '/' or '\\') && currentSegmentLength > 1)
else if (treatAsPath && (c is '/' or '\\') && currentSegmentLength > 1)
{
// if we treat this as a file name, we've started a new segment
b.Append(Path.DirectorySeparatorChar);
Expand Down Expand Up @@ -732,13 +737,12 @@ static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName
/// </summary>
public static string CleanUpDirectoryName(string text)
{
return CleanUpName(text, separateAtDots: false, treatAsFileName: false);
return CleanUpName(text, separateAtDots: false, treatAsFileName: false, treatAsPath: false);
}

public static string CleanUpPath(string text)
{
return CleanUpName(text, separateAtDots: true, treatAsFileName: false)
.Replace('.', Path.DirectorySeparatorChar);
return CleanUpName(text, separateAtDots: true, treatAsFileName: true, treatAsPath: true);
}

static bool IsReservedFileSystemName(string name)
Expand Down
2 changes: 1 addition & 1 deletion ICSharpCode.Decompiler/DebugInfo/PortablePdbWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ string BuildFileNameFromTypeName(TypeDefinitionHandle handle)
string ns = settings.UseNestedDirectoriesForNamespaces
? WholeProjectDecompiler.CleanUpPath(typeName.Namespace)
: WholeProjectDecompiler.CleanUpDirectoryName(typeName.Namespace);
return Path.Combine(ns, WholeProjectDecompiler.CleanUpFileName(typeName.Name) + ".cs");
return Path.Combine(ns, WholeProjectDecompiler.CleanUpFileName(typeName.Name, ".cs"));
}

var sourceFiles = reader.GetTopLevelTypeDefinitions().Where(t => IncludeTypeWhenGeneratingPdb(file, t, settings)).GroupBy(BuildFileNameFromTypeName).ToList();
Expand Down
2 changes: 1 addition & 1 deletion ILSpy.BamlDecompiler/BamlResourceNodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public string WriteResourceToFile(LoadedAssembly assembly, string fileName, Stre
var typeDefinition = result.TypeName.HasValue ? typeSystem.MainModule.GetTypeDefinition(result.TypeName.Value.TopLevelTypeName) : null;
if (typeDefinition != null)
{
fileName = WholeProjectDecompiler.CleanUpPath(typeDefinition.ReflectionName) + ".xaml";
fileName = WholeProjectDecompiler.CleanUpPath(typeDefinition.ReflectionName + ".xaml");
var partialTypeInfo = new PartialTypeInfo(typeDefinition);
foreach (var member in result.GeneratedMembers)
{
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Commands/GeneratePdbContextMenuEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal static void GeneratePdbForAssembly(LoadedAssembly assembly, LanguageSer
return;
}
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(assembly.ShortName) + ".pdb";
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(assembly.ShortName, ".pdb");
dlg.Filter = Resources.PortablePDBPdbAllFiles;
dlg.InitialDirectory = Path.GetDirectoryName(assembly.FileName);
if (dlg.ShowDialog() != true)
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Commands/SelectPdbContextMenuEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async void Execute(TextViewContext context)
if (assembly == null)
return;
OpenFileDialog dlg = new OpenFileDialog();
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(assembly.ShortName) + ".pdb";
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(assembly.ShortName, ".pdb");
dlg.Filter = Resources.PortablePDBPdbAllFiles;
dlg.InitialDirectory = Path.GetDirectoryName(assembly.FileName);
if (dlg.ShowDialog() != true)
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/TextView/DecompilerTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ public void SaveToDisk(ILSpy.Language language, IEnumerable<ILSpyTreeNode> treeN
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = language.FileExtension;
dlg.Filter = language.Name + "|*" + language.FileExtension + Properties.Resources.AllFiles;
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(treeNodes.First().ToString()) + language.FileExtension;
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(treeNodes.First().ToString(), language.FileExtension);
if (dlg.ShowDialog() == true)
{
SaveToDisk(new DecompilationContext(language, treeNodes.ToArray(), options), dlg.FileName);
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/TreeNodes/AssemblyTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public override bool Save(TabPageModel tabPage)
if (string.IsNullOrEmpty(language.ProjectFileExtension))
return false;
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(LoadedAssembly.ShortName) + language.ProjectFileExtension;
dlg.FileName = WholeProjectDecompiler.CleanUpFileName(LoadedAssembly.ShortName, language.ProjectFileExtension);
dlg.Filter = language.Name + " project|*" + language.ProjectFileExtension + "|" + language.Name + " single file|*" + language.FileExtension + "|All files|*.*";
if (dlg.ShowDialog() == true)
{
Expand Down

0 comments on commit 31bbcf4

Please sign in to comment.