Skip to content

Commit

Permalink
Support custom scripts with file name info
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
simonferquel committed Mar 16, 2021
1 parent f3ec06a commit 8871d88
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Mono.Cecil/IHaveAFileName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Mono.Cecil {
public interface IHaveAFileName {
string GetFileName ();
}
}
11 changes: 7 additions & 4 deletions Mono.Cecil/ModuleDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions Test/Mono.Cecil.Tests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
}
}
}
}

0 comments on commit 8871d88

Please sign in to comment.