Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8871d88

Browse files
committedMar 16, 2021
Support custom scripts with file name info
Many operations involved in rewriting debug symbols assume the rewritten assembly base stream is a FileStream to get file name information. This may not always be the case. To cover this, this changes the way file name is resolved so that a custom stream implementing `IHaveAFileName` can be understood by Cecil. Signed-off-by: Simon Ferquel <[email protected]>
1 parent f3ec06a commit 8871d88

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed
 

‎Mono.Cecil/IHaveAFileName.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Mono.Cecil {
6+
public interface IHaveAFileName {
7+
string GetFileName ();
8+
}
9+
}

‎Mono.Cecil/ModuleDefinition.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,11 +1289,14 @@ public static bool HasImage (this ModuleDefinition self)
12891289

12901290
public static string GetFileName (this Stream self)
12911291
{
1292-
var file_stream = self as FileStream;
1293-
if (file_stream == null)
1292+
switch (self) {
1293+
case FileStream fs:
1294+
return Path.GetFileName (fs.Name);
1295+
case IHaveAFileName withFileName:
1296+
return withFileName.GetFileName ();
1297+
default:
12941298
return string.Empty;
1295-
1296-
return Path.GetFullPath (file_stream.Name);
1299+
}
12971300
}
12981301

12991302
public static TargetRuntime ParseRuntime (this string self)

‎Test/Mono.Cecil.Tests/ModuleTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,20 @@ public void ExceptionInWriteDoesNotKeepLockOnFile ()
351351
// Ensure you can still delete the file
352352
File.Delete (path);
353353
}
354+
355+
class StreamWithAName : MemoryStream, IHaveAFileName {
356+
public string GetFileName ()
357+
{
358+
return "Yes I have!";
359+
}
360+
}
361+
362+
[Test]
363+
public void StreamImplementingIHaveAFileNameShouldReturnItAsIs ()
364+
{
365+
using (var stream = new StreamWithAName ()) {
366+
Assert.AreEqual ("Yes I have!", stream.GetFileName ());
367+
}
368+
}
354369
}
355370
}

0 commit comments

Comments
 (0)
Please sign in to comment.