Skip to content

Commit

Permalink
Fix #628: Add option to display metadata token in 10-based or hex for…
Browse files Browse the repository at this point in the history
…mat.
  • Loading branch information
siegfriedpammer committed Dec 23, 2018
1 parent 40dafd7 commit fbfcd6f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 32 deletions.
11 changes: 10 additions & 1 deletion ICSharpCode.Decompiler/Disassembler/MethodBodyDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class MethodBodyDisassembler
/// </summary>
public bool ShowMetadataTokens { get; set; }

/// <summary>
/// Show metadata tokens for instructions with token operands in base 10.
/// </summary>
public bool ShowMetadataTokensInBase10 { get; set; }

/// <summary>
/// Optional provider for sequence points.
/// </summary>
Expand Down Expand Up @@ -453,7 +458,11 @@ private void WriteMetadataToken(Handle? handle, int metadataToken, bool spaceBef
if (spaceBefore) {
output.Write(' ');
}
output.Write("/* {0:X8} */", metadataToken);
if (ShowMetadataTokensInBase10) {
output.Write("/* {0} */", metadataToken);
} else {
output.Write("/* {0:X8} */", metadataToken);
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public bool ShowMetadataTokens {
set => methodBodyDisassembler.ShowMetadataTokens = value;
}

public bool ShowMetadataTokensInBase10 {
get => methodBodyDisassembler.ShowMetadataTokensInBase10;
set => methodBodyDisassembler.ShowMetadataTokensInBase10 = value;
}

public IDebugInfoProvider DebugInfo {
get => methodBodyDisassembler.DebugInfo;
set => methodBodyDisassembler.DebugInfo = value;
Expand Down
42 changes: 15 additions & 27 deletions ILSpy/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,52 +101,40 @@ public static bool IsCustomAttribute(this TypeDefinition type)
return false;
}
*/
public static string ToSuffixString(this System.Reflection.Metadata.EntityHandle token)
public static string ToSuffixString(this System.Reflection.Metadata.EntityHandle handle)
{
if (!DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokens)
return string.Empty;

return " @" + System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(token).ToString("x8");
int token = System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(handle);
if (DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokensInBase10)
return " @" + token;
return " @" + token.ToString("x8");
}

public static string ToSuffixString(this System.Reflection.Metadata.MethodDefinitionHandle token)
public static string ToSuffixString(this System.Reflection.Metadata.MethodDefinitionHandle handle)
{
if (!DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokens)
return string.Empty;

return " @" + System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(token).ToString("x8");
return ToSuffixString((System.Reflection.Metadata.EntityHandle)handle);
}

public static string ToSuffixString(this System.Reflection.Metadata.PropertyDefinitionHandle token)
public static string ToSuffixString(this System.Reflection.Metadata.PropertyDefinitionHandle handle)
{
if (!DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokens)
return string.Empty;

return " @" + System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(token).ToString("x8");
return ToSuffixString((System.Reflection.Metadata.EntityHandle)handle);
}

public static string ToSuffixString(this System.Reflection.Metadata.EventDefinitionHandle token)
public static string ToSuffixString(this System.Reflection.Metadata.EventDefinitionHandle handle)
{
if (!DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokens)
return string.Empty;

return " @" + System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(token).ToString("x8");
return ToSuffixString((System.Reflection.Metadata.EntityHandle)handle);
}

public static string ToSuffixString(this System.Reflection.Metadata.FieldDefinitionHandle token)
public static string ToSuffixString(this System.Reflection.Metadata.FieldDefinitionHandle handle)
{
if (!DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokens)
return string.Empty;

return " @" + System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(token).ToString("x8");
return ToSuffixString((System.Reflection.Metadata.EntityHandle)handle);
}

public static string ToSuffixString(this System.Reflection.Metadata.TypeDefinitionHandle token)
public static string ToSuffixString(this System.Reflection.Metadata.TypeDefinitionHandle handle)
{
if (!DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokens)
return string.Empty;

return " @" + System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(token).ToString("x8");
return ToSuffixString((System.Reflection.Metadata.EntityHandle)handle);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions ILSpy/Languages/ILLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected virtual ReflectionDisassembler CreateDisassembler(ITextOutput output,
DetectControlStructure = detectControlStructure,
ShowSequencePoints = options.DecompilerSettings.ShowDebugInfo,
ShowMetadataTokens = Options.DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokens,
ShowMetadataTokensInBase10 = Options.DisplaySettingsPanel.CurrentDisplaySettings.ShowMetadataTokensInBase10,
ExpandMemberDefinitions = options.DecompilerSettings.ExpandMemberDefinitions
};
}
Expand Down
16 changes: 14 additions & 2 deletions ILSpy/Options/DisplaySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public bool ShowLineNumbers {

bool showMetadataTokens;

public bool ShowMetadataTokens
{
public bool ShowMetadataTokens {
get { return showMetadataTokens; }
set {
if (showMetadataTokens != value) {
Expand All @@ -94,6 +93,18 @@ public bool ShowMetadataTokens
}
}

bool showMetadataTokensInBase10;

public bool ShowMetadataTokensInBase10 {
get { return showMetadataTokensInBase10; }
set {
if (showMetadataTokensInBase10 != value) {
showMetadataTokensInBase10 = value;
OnPropertyChanged();
}
}
}

bool enableWordWrap;

public bool EnableWordWrap
Expand Down Expand Up @@ -126,6 +137,7 @@ public void CopyValues(DisplaySettings s)
this.SelectedFontSize = s.selectedFontSize;
this.ShowLineNumbers = s.showLineNumbers;
this.ShowMetadataTokens = s.showMetadataTokens;
this.ShowMetadataTokensInBase10 = s.showMetadataTokensInBase10;
this.EnableWordWrap = s.enableWordWrap;
this.SortResults = s.sortResults;
}
Expand Down
1 change: 1 addition & 0 deletions ILSpy/Options/DisplaySettingsPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<StackPanel Margin="3">
<CheckBox IsChecked="{Binding ShowLineNumbers}">Show line numbers</CheckBox>
<CheckBox IsChecked="{Binding ShowMetadataTokens}">Show metadata tokens</CheckBox>
<CheckBox IsChecked="{Binding ShowMetadataTokensInBase10}">Show metadata tokens in base 10</CheckBox>
<CheckBox IsChecked="{Binding EnableWordWrap}">Enable word wrap</CheckBox>
<CheckBox IsChecked="{Binding SortResults}">Sort results by fitness</CheckBox>
</StackPanel>
Expand Down
6 changes: 4 additions & 2 deletions ILSpy/Options/DisplaySettingsPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public static DisplaySettings LoadDisplaySettings(ILSpySettings settings)
s.SelectedFont = new FontFamily((string)e.Attribute("Font") ?? "Consolas");
s.SelectedFontSize = (double?)e.Attribute("FontSize") ?? 10.0 * 4 / 3;
s.ShowLineNumbers = (bool?)e.Attribute("ShowLineNumbers") ?? false;
s.ShowMetadataTokens = (bool?) e.Attribute("ShowMetadataTokens") ?? false;
s.EnableWordWrap = (bool?)e.Attribute("EnableWordWrap") ?? false;
s.ShowMetadataTokens = (bool?)e.Attribute("ShowMetadataTokens") ?? false;
s.ShowMetadataTokensInBase10 = (bool?)e.Attribute("ShowMetadataTokensInBase10") ?? false;
s.EnableWordWrap = (bool?)e.Attribute("EnableWordWrap") ?? false;
s.SortResults = (bool?)e.Attribute("SortResults") ?? true;

return s;
Expand All @@ -116,6 +117,7 @@ public void Save(XElement root)
section.SetAttributeValue("FontSize", s.SelectedFontSize);
section.SetAttributeValue("ShowLineNumbers", s.ShowLineNumbers);
section.SetAttributeValue("ShowMetadataTokens", s.ShowMetadataTokens);
section.SetAttributeValue("ShowMetadataTokensInBase10", s.ShowMetadataTokensInBase10);
section.SetAttributeValue("EnableWordWrap", s.EnableWordWrap);
section.SetAttributeValue("SortResults", s.SortResults);

Expand Down

0 comments on commit fbfcd6f

Please sign in to comment.