Skip to content

Commit

Permalink
Fix icsharpcode#2780: Show embedded resource size
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Sep 8, 2022
1 parent d631eed commit 3c0ab6a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
29 changes: 29 additions & 0 deletions ICSharpCode.Decompiler/Metadata/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public abstract class Resource
public virtual ManifestResourceAttributes Attributes => ManifestResourceAttributes.Public;
public abstract string Name { get; }
public abstract Stream? TryOpenStream();
public abstract long? TryGetLength();
}

public class ByteArrayResource : Resource
Expand All @@ -57,6 +58,11 @@ public override Stream TryOpenStream()
{
return new MemoryStream(data);
}

public override long? TryGetLength()
{
return data.Length;
}
}

sealed class MetadataResource : Resource
Expand Down Expand Up @@ -126,6 +132,29 @@ ResourceType GetResourceType()
return null;
return new ResourceMemoryStream(Module.Reader, ptr + sizeof(int), length);
}

public unsafe override long? TryGetLength()
{
if (ResourceType != ResourceType.Embedded)
return null;
var headers = Module.Reader.PEHeaders;
if (headers.CorHeader == null)
return null;
var resources = headers.CorHeader.ResourcesDirectory;
if (resources.RelativeVirtualAddress == 0)
return null;
var sectionData = Module.Reader.GetSectionData(resources.RelativeVirtualAddress);
if (sectionData.Length == 0)
return null;
var resource = Module.Metadata.GetManifestResource(Handle);
if (resource.Offset > sectionData.Length)
return null;
byte* ptr = sectionData.Pointer + resource.Offset;
int length = ptr[0] + (ptr[1] << 8) + (ptr[2] << 16) + (ptr[3] << 24);
if (length < 0 || length > sectionData.Length)
return null;
return length;
}
}

sealed unsafe class ResourceMemoryStream : UnmanagedMemoryStream
Expand Down
16 changes: 16 additions & 0 deletions ICSharpCode.ILSpyX/LoadedPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public FolderEntry(string name, PackageEntry originalEntry)
public override string FullName => originalEntry.FullName;
public override ResourceType ResourceType => originalEntry.ResourceType;
public override Stream? TryOpenStream() => originalEntry.TryOpenStream();
public override long? TryGetLength() => originalEntry.TryGetLength();
}

sealed class ZipFileEntry : PackageEntry
Expand Down Expand Up @@ -176,6 +177,16 @@ public ZipFileEntry(string zipFile, ZipArchiveEntry entry)
memoryStream.Position = 0;
return memoryStream;
}

public override long? TryGetLength()
{
Debug.WriteLine("TryGetLength " + Name);
using var archive = ZipFile.OpenRead(zipFile);
var entry = archive.GetEntry(Name);
if (entry == null)
return null;
return entry.Length;
}
}

sealed class BundleEntry : PackageEntry
Expand Down Expand Up @@ -217,6 +228,11 @@ public override Stream TryOpenStream()
return decompressedStream;
}
}

public override long? TryGetLength()
{
return entry.Size;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion ILSpy/TreeNodes/ResourceNodes/ResourceTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public override FilterResult Filter(FilterSettings settings)

public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
{
language.WriteCommentLine(output, string.Format("{0} ({1}, {2})", Resource.Name, Resource.ResourceType, Resource.Attributes));
var sizeInBytes = Resource.TryGetLength();
var sizeInBytesText = sizeInBytes == null ? "" : ", " + sizeInBytes + " bytes";
language.WriteCommentLine(output, $"{Resource.Name} ({Resource.ResourceType}, {Resource.Attributes}{sizeInBytesText})");

ISmartTextOutput smartOutput = output as ISmartTextOutput;
if (smartOutput != null)
Expand Down

0 comments on commit 3c0ab6a

Please sign in to comment.