From 8871d88a904c8aabf569d2c321d44ef864c973ee Mon Sep 17 00:00:00 2001 From: Simon Ferquel Date: Tue, 16 Mar 2021 22:45:55 +0100 Subject: [PATCH] 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 --- Mono.Cecil/IHaveAFileName.cs | 9 +++++++++ Mono.Cecil/ModuleDefinition.cs | 11 +++++++---- Test/Mono.Cecil.Tests/ModuleTests.cs | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 Mono.Cecil/IHaveAFileName.cs diff --git a/Mono.Cecil/IHaveAFileName.cs b/Mono.Cecil/IHaveAFileName.cs new file mode 100644 index 000000000..33b217e48 --- /dev/null +++ b/Mono.Cecil/IHaveAFileName.cs @@ -0,0 +1,9 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Mono.Cecil { + public interface IHaveAFileName { + string GetFileName (); + } +} diff --git a/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/ModuleDefinition.cs index 8fb35a540..c5d24827a 100644 --- a/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/ModuleDefinition.cs @@ -1289,11 +1289,14 @@ public static bool HasImage (this ModuleDefinition self) public static string GetFileName (this Stream self) { - var file_stream = self as FileStream; - if (file_stream == null) + switch (self) { + case FileStream fs: + return Path.GetFileName (fs.Name); + case IHaveAFileName withFileName: + return withFileName.GetFileName (); + default: return string.Empty; - - return Path.GetFullPath (file_stream.Name); + } } public static TargetRuntime ParseRuntime (this string self) diff --git a/Test/Mono.Cecil.Tests/ModuleTests.cs b/Test/Mono.Cecil.Tests/ModuleTests.cs index 5af2a65d7..f679fcfa1 100644 --- a/Test/Mono.Cecil.Tests/ModuleTests.cs +++ b/Test/Mono.Cecil.Tests/ModuleTests.cs @@ -351,5 +351,20 @@ public void ExceptionInWriteDoesNotKeepLockOnFile () // Ensure you can still delete the file File.Delete (path); } + + class StreamWithAName : MemoryStream, IHaveAFileName { + public string GetFileName () + { + return "Yes I have!"; + } + } + + [Test] + public void StreamImplementingIHaveAFileNameShouldReturnItAsIs () + { + using (var stream = new StreamWithAName ()) { + Assert.AreEqual ("Yes I have!", stream.GetFileName ()); + } + } } }