1919using System ;
2020using System . Collections . Concurrent ;
2121using System . Collections . Generic ;
22+ using System . Diagnostics ;
2223using System . IO ;
2324using System . Linq ;
2425using System . Reflection . Metadata ;
@@ -135,7 +136,7 @@ protected WholeProjectDecompiler(
135136
136137 public void DecompileProject ( MetadataFile file , string targetDirectory , CancellationToken cancellationToken = default ( CancellationToken ) )
137138 {
138- string projectFileName = Path . Combine ( targetDirectory , CleanUpFileName ( file . Name ) + ".csproj" ) ;
139+ string projectFileName = Path . Combine ( targetDirectory , CleanUpFileName ( file . Name , ".csproj" ) ) ;
139140 using ( var writer = new StreamWriter ( projectFileName ) )
140141 {
141142 DecompileProject ( file , targetDirectory , writer , cancellationToken ) ;
@@ -238,7 +239,7 @@ IEnumerable<ProjectItemInfo> WriteCodeFilesInProject(MetadataFile module, IList<
238239 string GetFileFileNameForHandle ( TypeDefinitionHandle h )
239240 {
240241 var type = metadata . GetTypeDefinition ( h ) ;
241- string file = CleanUpFileName ( metadata . GetString ( type . Name ) + ".cs" ) ;
242+ string file = CleanUpFileName ( metadata . GetString ( type . Name ) , ".cs" ) ;
242243 string ns = metadata . GetString ( type . Namespace ) ;
243244 if ( string . IsNullOrEmpty ( ns ) )
244245 {
@@ -339,8 +340,7 @@ protected virtual IEnumerable<ProjectItemInfo> WriteResourceFilesInProject(Metad
339340 {
340341 foreach ( var ( name , value ) in resourcesFile )
341342 {
342- string fileName = SanitizeFileName ( name )
343- . Replace ( '/' , Path . DirectorySeparatorChar ) ;
343+ string fileName = SanitizeFileName ( name ) ;
344344 string dirName = Path . GetDirectoryName ( fileName ) ;
345345 if ( ! string . IsNullOrEmpty ( dirName ) && directories . Add ( dirName ) )
346346 {
@@ -609,9 +609,14 @@ static string CleanUpApplicationManifest(byte[] appManifest)
609609 /// <summary>
610610 /// Cleans up a node name for use as a file name.
611611 /// </summary>
612- public static string CleanUpFileName ( string text )
612+ public static string CleanUpFileName ( string text , string extension )
613613 {
614- return CleanUpName ( text , separateAtDots : false , treatAsFileName : false ) ;
614+ Debug . Assert ( ! string . IsNullOrEmpty ( extension ) ) ;
615+ if ( ! extension . StartsWith ( "." ) )
616+ extension = "." + extension ;
617+ text = text + extension ;
618+
619+ return CleanUpName ( text , separateAtDots : false , treatAsFileName : ! string . IsNullOrEmpty ( extension ) , treatAsPath : false ) ;
615620 }
616621
617622 /// <summary>
@@ -620,7 +625,7 @@ public static string CleanUpFileName(string text)
620625 /// </summary>
621626 public static string SanitizeFileName ( string fileName )
622627 {
623- return CleanUpName ( fileName , separateAtDots : false , treatAsFileName : true ) ;
628+ return CleanUpName ( fileName , separateAtDots : false , treatAsFileName : true , treatAsPath : true ) ;
624629 }
625630
626631 /// <summary>
@@ -629,7 +634,7 @@ public static string SanitizeFileName(string fileName)
629634 /// If <paramref name="treatAsFileName"/> is active, we check for file a extension and try to preserve it,
630635 /// if it's valid.
631636 /// </summary>
632- static string CleanUpName ( string text , bool separateAtDots , bool treatAsFileName )
637+ static string CleanUpName ( string text , bool separateAtDots , bool treatAsFileName , bool treatAsPath )
633638 {
634639 // Remove anything that could be confused with a rooted path.
635640 int pos = text . IndexOf ( ':' ) ;
@@ -692,7 +697,7 @@ static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName
692697 if ( separateAtDots )
693698 currentSegmentLength = 0 ;
694699 }
695- else if ( treatAsFileName && ( c is '/' or '\\ ' ) && currentSegmentLength > 1 )
700+ else if ( treatAsPath && ( c is '/' or '\\ ' ) && currentSegmentLength > 1 )
696701 {
697702 // if we treat this as a file name, we've started a new segment
698703 b . Append ( Path . DirectorySeparatorChar ) ;
@@ -732,13 +737,12 @@ static string CleanUpName(string text, bool separateAtDots, bool treatAsFileName
732737 /// </summary>
733738 public static string CleanUpDirectoryName ( string text )
734739 {
735- return CleanUpName ( text , separateAtDots : false , treatAsFileName : false ) ;
740+ return CleanUpName ( text , separateAtDots : false , treatAsFileName : false , treatAsPath : false ) ;
736741 }
737742
738743 public static string CleanUpPath ( string text )
739744 {
740- return CleanUpName ( text , separateAtDots : true , treatAsFileName : false )
741- . Replace ( '.' , Path . DirectorySeparatorChar ) ;
745+ return CleanUpName ( text , separateAtDots : true , treatAsFileName : true , treatAsPath : true ) ;
742746 }
743747
744748 static bool IsReservedFileSystemName ( string name )
0 commit comments