Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA and CDI support for manifest resources #418

Merged
merged 3 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/DotNet/ModuleDefMD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,17 +1478,17 @@ Resource CreateResource(uint rid) {

if (token.Rid == 0) {
if (TryCreateResourceStream(mr.Offset, out var dataReaderFactory, out uint resourceOffset, out uint resourceLength))
return new EmbeddedResource(mr.Name, dataReaderFactory, resourceOffset, resourceLength, mr.Flags) { Rid = rid, Offset = mr.Offset };
return new EmbeddedResource(mr.Name, Array2.Empty<byte>(), mr.Flags) { Rid = rid, Offset = mr.Offset };
return new EmbeddedResourceMD(this, mr, dataReaderFactory, resourceOffset, resourceLength);
return new EmbeddedResourceMD(this, mr, Array2.Empty<byte>());
}

if (mr.Implementation is FileDef file)
return new LinkedResource(mr.Name, file, mr.Flags) { Rid = rid, Offset = mr.Offset };
return new LinkedResourceMD(this, mr, file);

if (mr.Implementation is AssemblyRef asmRef)
return new AssemblyLinkedResource(mr.Name, asmRef, mr.Flags) { Rid = rid, Offset = mr.Offset };
return new AssemblyLinkedResourceMD(this, mr, asmRef);

return new EmbeddedResource(mr.Name, Array2.Empty<byte>(), mr.Flags) { Rid = rid, Offset = mr.Offset };
return new EmbeddedResourceMD(this, mr, Array2.Empty<byte>());
}

[HandleProcessCorruptedStateExceptions, SecurityCritical] // Req'd on .NET 4.0
Expand Down
1 change: 1 addition & 0 deletions src/DotNet/ModuleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ void Load(Resource obj) {
Add(obj.Offset);
Add(obj.Name);
Add(obj.Attributes);
Add(obj.CustomAttributes);

switch (obj.ResourceType) {
case ResourceType.Embedded:
Expand Down
154 changes: 148 additions & 6 deletions src/DotNet/Resource.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// dnlib: See LICENSE.txt for more info

using System;
using System.Collections.Generic;
using System.Threading;
using dnlib.IO;
using dnlib.DotNet.MD;
using dnlib.DotNet.Pdb;

namespace dnlib.DotNet {
/// <summary>
Expand All @@ -28,9 +31,9 @@ public enum ResourceType {
/// <summary>
/// Resource base class
/// </summary>
public abstract class Resource : IMDTokenProvider {
uint rid;
uint? offset;
public abstract class Resource : IMDTokenProvider, IHasCustomAttribute, IHasCustomDebugInformation {
private protected uint rid;
private protected uint? offset;
UTF8String name;
ManifestResourceAttributes flags;

Expand Down Expand Up @@ -90,6 +93,50 @@ public ManifestResourceAttributes Visibility {
/// </summary>
public bool IsPrivate => (flags & ManifestResourceAttributes.VisibilityMask) == ManifestResourceAttributes.Private;

/// <inheritdoc/>
public int HasCustomAttributeTag => 18;

/// <summary>
/// Gets all custom attributes
/// </summary>
public CustomAttributeCollection CustomAttributes {
get {
if (customAttributes is null)
InitializeCustomAttributes();
return customAttributes;
}
}
/// <summary/>
protected CustomAttributeCollection customAttributes;
/// <summary>Initializes <see cref="customAttributes"/></summary>
protected virtual void InitializeCustomAttributes() =>
Interlocked.CompareExchange(ref customAttributes, new CustomAttributeCollection(), null);

/// <inheritdoc/>
public bool HasCustomAttributes => CustomAttributes.Count > 0;

/// <inheritdoc/>
public int HasCustomDebugInformationTag => 18;

/// <summary>
/// Gets all custom debug infos
/// </summary>
public IList<PdbCustomDebugInfo> CustomDebugInfos {
get {
if (customDebugInfos is null)
InitializeCustomDebugInfos();
return customDebugInfos;
}
}
/// <summary/>
protected IList<PdbCustomDebugInfo> customDebugInfos;
/// <summary>Initializes <see cref="customDebugInfos"/></summary>
protected virtual void InitializeCustomDebugInfos() =>
Interlocked.CompareExchange(ref customDebugInfos, new List<PdbCustomDebugInfo>(), null);

/// <inheritdoc/>
public bool HasCustomDebugInfos => CustomDebugInfos.Count > 0;

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -104,7 +151,7 @@ protected Resource(UTF8String name, ManifestResourceAttributes flags) {
/// <summary>
/// A resource that is embedded in a .NET module. This is the most common type of resource.
/// </summary>
public sealed class EmbeddedResource : Resource {
public class EmbeddedResource : Resource {
readonly DataReaderFactory dataReaderFactory;
readonly uint resourceStartOffset;
readonly uint resourceLength;
Expand Down Expand Up @@ -152,10 +199,45 @@ public EmbeddedResource(UTF8String name, DataReaderFactory dataReaderFactory, ui
public override string ToString() => $"{UTF8String.ToSystemStringOrEmpty(Name)} - size: {(resourceLength)}";
}

sealed class EmbeddedResourceMD : EmbeddedResource, IMDTokenProviderMD {
/// <summary>The module where this instance is located</summary>
readonly ModuleDefMD readerModule;

readonly uint origRid;

/// <inheritdoc/>
public uint OrigRid => origRid;

/// <inheritdoc/>
protected override void InitializeCustomAttributes() {
var list = readerModule.Metadata.GetCustomAttributeRidList(Table.ManifestResource, origRid);
var tmp = new CustomAttributeCollection(list.Count, list, (list2, index) => readerModule.ReadCustomAttribute(list[index]));
Interlocked.CompareExchange(ref customAttributes, tmp, null);
}

/// <inheritdoc/>
protected override void InitializeCustomDebugInfos() {
var list = new List<PdbCustomDebugInfo>();
readerModule.InitializeCustomDebugInfos(new MDToken(MDToken.Table, origRid), new GenericParamContext(), list);
Interlocked.CompareExchange(ref customDebugInfos, list, null);
}

public EmbeddedResourceMD(ModuleDefMD readerModule, ManifestResource mr, byte[] data)
: this(readerModule, mr, ByteArrayDataReaderFactory.Create(data, filename: null), 0, (uint)data.Length) {
}

public EmbeddedResourceMD(ModuleDefMD readerModule, ManifestResource mr, DataReaderFactory dataReaderFactory, uint offset, uint length)
: base(mr.Name, dataReaderFactory, offset, length, mr.Flags) {
this.readerModule = readerModule;
origRid = rid = mr.Rid;
this.offset = mr.Offset;
}
}

/// <summary>
/// A reference to a resource in another assembly
/// </summary>
public sealed class AssemblyLinkedResource : Resource {
public class AssemblyLinkedResource : Resource {
AssemblyRef asmRef;

/// <inheritdoc/>
Expand All @@ -182,10 +264,40 @@ public AssemblyLinkedResource(UTF8String name, AssemblyRef asmRef, ManifestResou
public override string ToString() => $"{UTF8String.ToSystemStringOrEmpty(Name)} - assembly: {asmRef.FullName}";
}

sealed class AssemblyLinkedResourceMD : AssemblyLinkedResource, IMDTokenProviderMD {
/// <summary>The module where this instance is located</summary>
readonly ModuleDefMD readerModule;

readonly uint origRid;

/// <inheritdoc/>
public uint OrigRid => origRid;

/// <inheritdoc/>
protected override void InitializeCustomAttributes() {
var list = readerModule.Metadata.GetCustomAttributeRidList(Table.ManifestResource, origRid);
var tmp = new CustomAttributeCollection(list.Count, list, (list2, index) => readerModule.ReadCustomAttribute(list[index]));
Interlocked.CompareExchange(ref customAttributes, tmp, null);
}

/// <inheritdoc/>
protected override void InitializeCustomDebugInfos() {
var list = new List<PdbCustomDebugInfo>();
readerModule.InitializeCustomDebugInfos(new MDToken(MDToken.Table, origRid), new GenericParamContext(), list);
Interlocked.CompareExchange(ref customDebugInfos, list, null);
}

public AssemblyLinkedResourceMD(ModuleDefMD readerModule, ManifestResource mr, AssemblyRef asmRef) : base(mr.Name, asmRef, mr.Flags) {
this.readerModule = readerModule;
origRid = rid = mr.Rid;
offset = mr.Offset;
}
}

/// <summary>
/// A resource that is stored in a file on disk
/// </summary>
public sealed class LinkedResource : Resource {
public class LinkedResource : Resource {
FileDef file;

/// <inheritdoc/>
Expand Down Expand Up @@ -224,4 +336,34 @@ public LinkedResource(UTF8String name, FileDef file, ManifestResourceAttributes
/// <inheritdoc/>
public override string ToString() => $"{UTF8String.ToSystemStringOrEmpty(Name)} - file: {UTF8String.ToSystemStringOrEmpty(FileName)}";
}

sealed class LinkedResourceMD : LinkedResource, IMDTokenProviderMD {
/// <summary>The module where this instance is located</summary>
readonly ModuleDefMD readerModule;

readonly uint origRid;

/// <inheritdoc/>
public uint OrigRid => origRid;

/// <inheritdoc/>
protected override void InitializeCustomAttributes() {
var list = readerModule.Metadata.GetCustomAttributeRidList(Table.ManifestResource, origRid);
var tmp = new CustomAttributeCollection(list.Count, list, (list2, index) => readerModule.ReadCustomAttribute(list[index]));
Interlocked.CompareExchange(ref customAttributes, tmp, null);
}

/// <inheritdoc/>
protected override void InitializeCustomDebugInfos() {
var list = new List<PdbCustomDebugInfo>();
readerModule.InitializeCustomDebugInfos(new MDToken(MDToken.Table, origRid), new GenericParamContext(), list);
Interlocked.CompareExchange(ref customDebugInfos, list, null);
}

public LinkedResourceMD(ModuleDefMD readerModule, ManifestResource mr, FileDef file) : base(mr.Name, file, mr.Flags) {
this.readerModule = readerModule;
origRid = rid = mr.Rid;
offset = mr.Offset;
}
}
}
12 changes: 6 additions & 6 deletions src/DotNet/Writer/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2980,8 +2980,8 @@ uint AddEmbeddedResource(EmbeddedResource er) {
rid = tablesHeap.ManifestResourceTable.Add(row);
manifestResourceInfos.Add(er, rid);
embeddedResourceToByteArray[er] = netResources.Add(er.CreateReader());
//TODO: Add custom attributes
//TODO: Add custom debug infos
AddCustomAttributes(Table.ManifestResource, rid, er);
AddCustomDebugInformationList(Table.ManifestResource, rid, er);
return rid;
}

Expand All @@ -2999,8 +2999,8 @@ uint AddAssemblyLinkedResource(AssemblyLinkedResource alr) {
AddImplementation(alr.Assembly));
rid = tablesHeap.ManifestResourceTable.Add(row);
manifestResourceInfos.Add(alr, rid);
//TODO: Add custom attributes
//TODO: Add custom debug infos
AddCustomAttributes(Table.ManifestResource, rid, alr);
AddCustomDebugInformationList(Table.ManifestResource, rid, alr);
return rid;
}

Expand All @@ -3018,8 +3018,8 @@ uint AddLinkedResource(LinkedResource lr) {
AddImplementation(lr.File));
rid = tablesHeap.ManifestResourceTable.Add(row);
manifestResourceInfos.Add(lr, rid);
//TODO: Add custom attributes
//TODO: Add custom debug infos
AddCustomAttributes(Table.ManifestResource, rid, lr);
AddCustomDebugInformationList(Table.ManifestResource, rid, lr);
return rid;
}

Expand Down