Skip to content

Commit

Permalink
Escape resource names in the treeview and decompiler (fixes #387)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElektroKill committed Dec 21, 2024
1 parent e62233e commit 2fb9073
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ protected ResourceElementNode(ITreeNodeGroup treeNodeGroup, ResourceElement reso
public virtual void WriteShort(IDecompilerOutput output, IDecompiler decompiler, bool showOffset) {
decompiler.WriteCommentBegin(output, true);
output.WriteOffsetComment(this, showOffset);
const string LTR = "\u200E";
output.Write(NameUtilities.CleanName(Name) + LTR, this, DecompilerReferenceFlags.Local | DecompilerReferenceFlags.Definition, BoxedTextColor.Comment);
output.Write(IdentifierEscaper.Escape(Name), this, DecompilerReferenceFlags.Local | DecompilerReferenceFlags.Definition, BoxedTextColor.Comment);
output.Write($" = {ValueString}", BoxedTextColor.Comment);
decompiler.WriteCommentEnd(output, true);
output.WriteLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class ResourceNode : DocumentTreeNodeData, IResourceNode {

/// <inheritdoc/>
protected sealed override void WriteCore(ITextColorWriter output, IDecompiler decompiler, DocumentNodeWriteOptions options) {
output.WriteFilename(Resource.Name);
output.WriteFilenameIdentifier(Resource.Name);
if ((options & DocumentNodeWriteOptions.ToolTip) != 0) {
output.WriteLine();
WriteFilename(output);
Expand Down Expand Up @@ -149,8 +149,7 @@ protected void Save() =>
public virtual void WriteShort(IDecompilerOutput output, IDecompiler decompiler, bool showOffset) {
decompiler.WriteCommentBegin(output, true);
output.WriteOffsetComment(this, showOffset);
const string LTR = "\u200E";
output.Write(NameUtilities.CleanName(Name) + LTR, this, DecompilerReferenceFlags.Local | DecompilerReferenceFlags.Definition, BoxedTextColor.Comment);
output.Write(IdentifierEscaper.Escape(Name), this, DecompilerReferenceFlags.Local | DecompilerReferenceFlags.Definition, BoxedTextColor.Comment);
string? extra = null;
switch (Resource.ResourceType) {
case ResourceType.AssemblyLinked:
Expand Down
33 changes: 33 additions & 0 deletions dnSpy/dnSpy.Contracts.Logic/Text/ITextColorWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,38 @@ public static T WriteFilename<T>(this T output, string? filename) where T : ITex
}
return output;
}

/// <summary>
/// Writes a filename, escaping the filename and directory parts using <see cref="IdentifierEscaper"/>
/// </summary>
/// <typeparam name="T">Writer type</typeparam>
/// <param name="output">Output</param>
/// <param name="filename">Filename</param>
/// <returns></returns>
public static T WriteFilenameIdentifier<T>(this T output, string? filename) where T : ITextColorWriter {
if (filename is null)
return output;
var s = filename.Replace('\\', '/');
var parts = s.Split('/');
int slashIndex = 0;
for (int i = 0; i < parts.Length - 1; i++) {
output.Write(BoxedTextColor.DirectoryPart, IdentifierEscaper.Escape(parts[i]));
slashIndex += parts[i].Length;
output.Write(BoxedTextColor.Text, filename[slashIndex].ToString());
slashIndex++;
}
var fn = parts[parts.Length - 1];
int index = fn.LastIndexOf('.');
if (index < 0)
output.Write(BoxedTextColor.FileNameNoExtension, IdentifierEscaper.Escape(fn));
else {
string ext = fn.Substring(index + 1);
fn = fn.Substring(0, index);
output.Write(BoxedTextColor.FileNameNoExtension, IdentifierEscaper.Escape(fn));
output.Write(BoxedTextColor.Text, ".");
output.Write(BoxedTextColor.FileExtension, ext);
}
return output;
}
}
}

0 comments on commit 2fb9073

Please sign in to comment.