// dnlib: See LICENSE.txt for more info
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using dnlib.DotNet.MD;
using dnlib.DotNet.Pdb;
namespace dnlib.DotNet {
///
/// A high-level representation of a row in the File table
///
public abstract class FileDef : IHasCustomAttribute, IImplementation, IHasCustomDebugInformation, IManagedEntryPoint {
///
/// The row id in its table
///
protected uint rid;
///
public MDToken MDToken => new MDToken(Table.File, rid);
///
public uint Rid {
get => rid;
set => rid = value;
}
///
public int HasCustomAttributeTag => 16;
///
public int ImplementationTag => 0;
///
/// From column File.Flags
///
public FileAttributes Flags {
get => (FileAttributes)attributes;
set => attributes = (int)value;
}
/// Attributes
protected int attributes;
///
/// From column File.Name
///
public UTF8String Name {
get => name;
set => name = value;
}
/// Name
protected UTF8String name;
///
/// From column File.HashValue
///
public byte[] HashValue {
get => hashValue;
set => hashValue = value;
}
///
protected byte[] hashValue;
///
/// Gets all custom attributes
///
public CustomAttributeCollection CustomAttributes {
get {
if (customAttributes is null)
InitializeCustomAttributes();
return customAttributes;
}
}
///
protected CustomAttributeCollection customAttributes;
/// Initializes
protected virtual void InitializeCustomAttributes() =>
Interlocked.CompareExchange(ref customAttributes, new CustomAttributeCollection(), null);
///
public bool HasCustomAttributes => CustomAttributes.Count > 0;
///
public int HasCustomDebugInformationTag => 16;
///
public bool HasCustomDebugInfos => CustomDebugInfos.Count > 0;
///
/// Gets all custom debug infos
///
public IList CustomDebugInfos {
get {
if (customDebugInfos is null)
InitializeCustomDebugInfos();
return customDebugInfos;
}
}
///
protected IList customDebugInfos;
/// Initializes
protected virtual void InitializeCustomDebugInfos() =>
Interlocked.CompareExchange(ref customDebugInfos, new List(), null);
///
/// Set or clear flags in
///
/// true if flags should be set, false if flags should
/// be cleared
/// Flags to set or clear
void ModifyAttributes(bool set, FileAttributes flags) {
if (set)
attributes |= (int)flags;
else
attributes &= ~(int)flags;
}
///
/// Gets/sets the bit
///
public bool ContainsMetadata {
get => ((FileAttributes)attributes & FileAttributes.ContainsNoMetadata) == 0;
set => ModifyAttributes(!value, FileAttributes.ContainsNoMetadata);
}
///
/// Gets/sets the bit
///
public bool ContainsNoMetadata {
get => ((FileAttributes)attributes & FileAttributes.ContainsNoMetadata) != 0;
set => ModifyAttributes(value, FileAttributes.ContainsNoMetadata);
}
///
public string FullName => UTF8String.ToSystemStringOrEmpty(name);
///
public override string ToString() => FullName;
}
///
/// A File row created by the user and not present in the original .NET file
///
public class FileDefUser : FileDef {
///
/// Default constructor
///
public FileDefUser() {
}
///
/// Constructor
///
/// Name of file
/// Flags
/// File hash
public FileDefUser(UTF8String name, FileAttributes flags, byte[] hashValue) {
this.name = name;
attributes = (int)flags;
this.hashValue = hashValue;
}
}
///
/// Created from a row in the File table
///
sealed class FileDefMD : FileDef, IMDTokenProviderMD {
/// The module where this instance is located
readonly ModuleDefMD readerModule;
readonly uint origRid;
///
public uint OrigRid => origRid;
///
protected override void InitializeCustomAttributes() {
var list = readerModule.Metadata.GetCustomAttributeRidList(Table.File, origRid);
var tmp = new CustomAttributeCollection(list.Count, list, (list2, index) => readerModule.ReadCustomAttribute(list[index]));
Interlocked.CompareExchange(ref customAttributes, tmp, null);
}
///
protected override void InitializeCustomDebugInfos() {
var list = new List();
readerModule.InitializeCustomDebugInfos(new MDToken(MDToken.Table, origRid), new GenericParamContext(), list);
Interlocked.CompareExchange(ref customDebugInfos, list, null);
}
///
/// Constructor
///
/// The module which contains this File row
/// Row ID
/// If is null
/// If is invalid
public FileDefMD(ModuleDefMD readerModule, uint rid) {
#if DEBUG
if (readerModule is null)
throw new ArgumentNullException("readerModule");
if (readerModule.TablesStream.FileTable.IsInvalidRID(rid))
throw new BadImageFormatException($"File rid {rid} does not exist");
#endif
origRid = rid;
this.rid = rid;
this.readerModule = readerModule;
bool b = readerModule.TablesStream.TryReadFileRow(origRid, out var row);
Debug.Assert(b);
attributes = (int)row.Flags;
name = readerModule.StringsStream.ReadNoNull(row.Name);
hashValue = readerModule.BlobStream.Read(row.HashValue);
}
}
}