Skip to content

Commit

Permalink
Clean up node classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Dec 31, 2023
1 parent 942131f commit 12cbf14
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 260 deletions.
10 changes: 10 additions & 0 deletions ICSharpCode.Decompiler/Metadata/PEFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ namespace ICSharpCode.Decompiler.Metadata
{
public class MetadataFile
{
public enum MetadataFileKind
{
PortableExecutable,
ProgramDebugDatabase,
Metadata
}

public string FileName { get; }
public MetadataFileKind Kind { get; }
public MetadataReader Metadata { get; }

public virtual int MetadataOffset { get; }
Expand All @@ -46,6 +54,7 @@ public MetadataFile(string fileName, MetadataReaderProvider metadata, MetadataRe
this.Metadata = metadata.GetMetadataReader(metadataOptions);
this.MetadataOffset = metadataOffset;
this.IsEmbedded = isEmbedded;
this.Kind = isEmbedded || Path.GetExtension(fileName).Equals(".pdb", StringComparison.OrdinalIgnoreCase) ? MetadataFileKind.ProgramDebugDatabase : MetadataFileKind.Metadata;
}

private protected MetadataFile(string fileName, PEReader reader, MetadataReaderOptions metadataOptions = MetadataReaderOptions.Default)
Expand All @@ -55,6 +64,7 @@ private protected MetadataFile(string fileName, PEReader reader, MetadataReaderO
if (!reader.HasMetadata)
throw new PEFileNotSupportedException("PE file does not contain any managed metadata.");
this.Metadata = reader.GetMetadataReader(metadataOptions);
this.Kind = MetadataFileKind.PortableExecutable;
}
}

Expand Down
47 changes: 25 additions & 22 deletions ICSharpCode.ILSpyX/LoadedAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,31 +268,34 @@ public string Text {
if (IsLoaded && !HasLoadError)
{
var result = GetLoadResultAsync().GetAwaiter().GetResult();
if (result.PEFile != null)
if (result.MetadataFile != null)
{
var metadata = result.PEFile.Metadata;
string? versionOrInfo = null;
if (metadata != null)
switch (result.MetadataFile.Kind)
{
if (metadata.IsAssembly)
{
versionOrInfo = metadata.GetAssemblyDefinition().Version?.ToString();
string tfId = GetTargetFrameworkIdAsync().GetAwaiter().GetResult();
if (!string.IsNullOrEmpty(tfId))
versionOrInfo += ", " + tfId.Replace("Version=", " ");
}
else
{
versionOrInfo = ".netmodule";
}
case MetadataFile.MetadataFileKind.PortableExecutable:
var metadata = result.MetadataFile.Metadata;
string? versionOrInfo;
if (metadata.IsAssembly)
{
versionOrInfo = metadata.GetAssemblyDefinition().Version?.ToString();
string tfId = GetTargetFrameworkIdAsync().GetAwaiter().GetResult();
if (!string.IsNullOrEmpty(tfId))
versionOrInfo += ", " + tfId.Replace("Version=", " ");
}
else
{
versionOrInfo = ".netmodule";
}
if (versionOrInfo == null)
return ShortName;
return string.Format("{0} ({1})", ShortName, versionOrInfo);
case MetadataFile.MetadataFileKind.ProgramDebugDatabase:
return ShortName + " (Debug Metadata)";
case MetadataFile.MetadataFileKind.Metadata:
return ShortName + " (Metadata)";
default:
return ShortName;
}
if (versionOrInfo == null)
return ShortName;
return string.Format("{0} ({1})", ShortName, versionOrInfo);
}
else if (result.MetadataFile != null)
{
return ShortName + " (Metadata)";
}
}
return ShortName;
Expand Down
5 changes: 3 additions & 2 deletions ILSpy/Metadata/DebugDirectoryTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ protected override void LoadChildren()
break;

case DebugDirectoryEntryType.EmbeddedPortablePdb:
var embeddedPortablePdbReader = module.Reader.ReadEmbeddedPortablePdbDebugDirectoryData(entry).GetMetadataReader();
this.Children.Add(new DebugMetadataTreeNode(module));
var embeddedPortablePdbProvider = module.Reader.ReadEmbeddedPortablePdbDebugDirectoryData(entry);
var embeddedPortablePdbMetadataFile = new MetadataFile(module.FileName, embeddedPortablePdbProvider, isEmbedded: true);
this.Children.Add(new MetadataTreeNode(embeddedPortablePdbMetadataFile, "Debug Metadata (Embedded)"));
break;

case DebugDirectoryEntryType.PdbChecksum:
Expand Down
77 changes: 0 additions & 77 deletions ILSpy/Metadata/DebugMetadataTablesTreeNode.cs

This file was deleted.

71 changes: 0 additions & 71 deletions ILSpy/Metadata/DebugMetadataTreeNode.cs

This file was deleted.

73 changes: 5 additions & 68 deletions ILSpy/Metadata/MetadataTablesTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,74 +43,11 @@ public MetadataTablesTreeNode(MetadataFile metadataFile)

protected override void LoadChildren()
{
if (ShowTable(TableIndex.Module, metadataFile.Metadata))
this.Children.Add(new ModuleTableTreeNode(metadataFile));
if (ShowTable(TableIndex.TypeRef, metadataFile.Metadata))
this.Children.Add(new TypeRefTableTreeNode(metadataFile));
if (ShowTable(TableIndex.TypeDef, metadataFile.Metadata))
this.Children.Add(new TypeDefTableTreeNode(metadataFile));
if (ShowTable(TableIndex.Field, metadataFile.Metadata))
this.Children.Add(new FieldTableTreeNode(metadataFile));
if (ShowTable(TableIndex.MethodDef, metadataFile.Metadata))
this.Children.Add(new MethodTableTreeNode(metadataFile));
if (ShowTable(TableIndex.Param, metadataFile.Metadata))
this.Children.Add(new ParamTableTreeNode(metadataFile));
if (ShowTable(TableIndex.InterfaceImpl, metadataFile.Metadata))
this.Children.Add(new InterfaceImplTableTreeNode(metadataFile));
if (ShowTable(TableIndex.MemberRef, metadataFile.Metadata))
this.Children.Add(new MemberRefTableTreeNode(metadataFile));
if (ShowTable(TableIndex.Constant, metadataFile.Metadata))
this.Children.Add(new ConstantTableTreeNode(metadataFile));
if (ShowTable(TableIndex.CustomAttribute, metadataFile.Metadata))
this.Children.Add(new CustomAttributeTableTreeNode(metadataFile));
if (ShowTable(TableIndex.FieldMarshal, metadataFile.Metadata))
this.Children.Add(new FieldMarshalTableTreeNode(metadataFile));
if (ShowTable(TableIndex.DeclSecurity, metadataFile.Metadata))
this.Children.Add(new DeclSecurityTableTreeNode(metadataFile));
if (ShowTable(TableIndex.ClassLayout, metadataFile.Metadata))
this.Children.Add(new ClassLayoutTableTreeNode(metadataFile));
if (ShowTable(TableIndex.FieldLayout, metadataFile.Metadata))
this.Children.Add(new FieldLayoutTableTreeNode(metadataFile));
if (ShowTable(TableIndex.StandAloneSig, metadataFile.Metadata))
this.Children.Add(new StandAloneSigTableTreeNode(metadataFile));
if (ShowTable(TableIndex.EventMap, metadataFile.Metadata))
this.Children.Add(new EventMapTableTreeNode(metadataFile));
if (ShowTable(TableIndex.Event, metadataFile.Metadata))
this.Children.Add(new EventTableTreeNode(metadataFile));
if (ShowTable(TableIndex.PropertyMap, metadataFile.Metadata))
this.Children.Add(new PropertyMapTableTreeNode(metadataFile));
if (ShowTable(TableIndex.Property, metadataFile.Metadata))
this.Children.Add(new PropertyTableTreeNode(metadataFile));
if (ShowTable(TableIndex.MethodSemantics, metadataFile.Metadata))
this.Children.Add(new MethodSemanticsTableTreeNode(metadataFile));
if (ShowTable(TableIndex.MethodImpl, metadataFile.Metadata))
this.Children.Add(new MethodImplTableTreeNode(metadataFile));
if (ShowTable(TableIndex.ModuleRef, metadataFile.Metadata))
this.Children.Add(new ModuleRefTableTreeNode(metadataFile));
if (ShowTable(TableIndex.TypeSpec, metadataFile.Metadata))
this.Children.Add(new TypeSpecTableTreeNode(metadataFile));
if (ShowTable(TableIndex.ImplMap, metadataFile.Metadata))
this.Children.Add(new ImplMapTableTreeNode(metadataFile));
if (ShowTable(TableIndex.FieldRva, metadataFile.Metadata))
this.Children.Add(new FieldRVATableTreeNode(metadataFile));
if (ShowTable(TableIndex.Assembly, metadataFile.Metadata))
this.Children.Add(new AssemblyTableTreeNode(metadataFile));
if (ShowTable(TableIndex.AssemblyRef, metadataFile.Metadata))
this.Children.Add(new AssemblyRefTableTreeNode(metadataFile));
if (ShowTable(TableIndex.File, metadataFile.Metadata))
this.Children.Add(new FileTableTreeNode(metadataFile));
if (ShowTable(TableIndex.ExportedType, metadataFile.Metadata))
this.Children.Add(new ExportedTypeTableTreeNode(metadataFile));
if (ShowTable(TableIndex.ManifestResource, metadataFile.Metadata))
this.Children.Add(new ManifestResourceTableTreeNode(metadataFile));
if (ShowTable(TableIndex.NestedClass, metadataFile.Metadata))
this.Children.Add(new NestedClassTableTreeNode(metadataFile));
if (ShowTable(TableIndex.GenericParam, metadataFile.Metadata))
this.Children.Add(new GenericParamTableTreeNode(metadataFile));
if (ShowTable(TableIndex.MethodSpec, metadataFile.Metadata))
this.Children.Add(new MethodSpecTableTreeNode(metadataFile));
if (ShowTable(TableIndex.GenericParamConstraint, metadataFile.Metadata))
this.Children.Add(new GenericParamConstraintTableTreeNode(metadataFile));
foreach (var table in Enum.GetValues<TableIndex>())
{
if (ShowTable(table, metadataFile.Metadata))
this.Children.Add(CreateTableTreeNode(table, metadataFile));
}
}

internal static bool ShowTable(TableIndex table, MetadataReader metadata) => !MainWindow.Instance.CurrentDisplaySettings.HideEmptyMetadataTables || metadata.GetTableRowCount(table) > 0;
Expand Down
Loading

0 comments on commit 12cbf14

Please sign in to comment.