Skip to content

Commit

Permalink
Merge pull request #383 from Washi1337/development
Browse files Browse the repository at this point in the history
5.0.0
  • Loading branch information
Washi1337 authored Dec 7, 2022
2 parents 76a5ed2 + 6fa174b commit 72f9b45
Show file tree
Hide file tree
Showing 90 changed files with 1,421 additions and 470 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RepositoryUrl>https://github.com/Washi1337/AsmResolver</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<LangVersion>10</LangVersion>
<Version>5.0.0-beta.2</Version>
<Version>5.0.0</Version>
</PropertyGroup>

</Project>
3 changes: 2 additions & 1 deletion src/AsmResolver.DotNet.Dynamic/DynamicCilOperandResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public DynamicCilOperandResolver(SerializedModuleDefinition contextModule, CilMe

case TableIndex.StandAloneSig:
var reader = new BinaryStreamReader((byte[])_tokens[(int)token.Rid]!);
return CallingConventionSignature.FromReader(new BlobReadContext(_readerContext), ref reader);
var blobReadContext = new BlobReaderContext(_readerContext);
return CallingConventionSignature.FromReader(ref blobReadContext, ref reader);
}

return token;
Expand Down
4 changes: 2 additions & 2 deletions src/AsmResolver.DotNet.Dynamic/DynamicMethodHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static void ReadLocalVariables(CilMethodBody methodBody, MethodDefinition
throw new ArgumentException("Method body should reference a serialized module.");

var reader = new BinaryStreamReader(localSig);
var context = new BlobReadContext(module.ReaderContext, DynamicTypeSignatureResolver.Instance);
if (CallingConventionSignature.FromReader(context, ref reader)
var context = new BlobReaderContext(module.ReaderContext, DynamicTypeSignatureResolver.Instance);
if (CallingConventionSignature.FromReader(ref context, ref reader)
is not LocalVariablesSignature localsSignature)
{
throw new ArgumentException("Invalid local variables signature.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static DynamicTypeSignatureResolver()
public static bool IsSupported => GetTypeFromHandleUnsafeMethod is not null;

/// <inheritdoc />
public override TypeSignature ResolveRuntimeType(in BlobReadContext context, nint address)
public override TypeSignature ResolveRuntimeType(ref BlobReaderContext context, nint address)
{
if (!IsSupported)
throw new PlatformNotSupportedException("The current platform does not support the translation of raw type handles to System.Type instances.");
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/AsmResolver.DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="6.0.6" />
<PackageReference Include="System.Text.Json" Version="6.0.7" />
</ItemGroup>

</Project>
10 changes: 4 additions & 6 deletions src/AsmResolver.DotNet/AssemblyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected AssemblyDefinition(MetadataToken token)
/// </summary>
/// <param name="name">The name of the assembly.</param>
/// <param name="version">The version of the assembly.</param>
public AssemblyDefinition(string? name, Version version)
public AssemblyDefinition(Utf8String? name, Version version)
: this(new MetadataToken(TableIndex.Assembly, 0))
{
Name = name;
Expand Down Expand Up @@ -290,11 +290,9 @@ public void Write(string filePath, IPEImageBuilder imageBuilder, IPEFileBuilder
for (int i = 0; i < Modules.Count; i++)
{
var module = Modules[i];
string modulePath;
if (module == ManifestModule)
modulePath = filePath;
else
modulePath = Path.Combine(directory, module.Name ?? $"module{i}.bin");
string modulePath = module == ManifestModule
? filePath
: Path.Combine(directory, module.Name?.Value ?? $"module{i}.bin");

module.Write(modulePath, imageBuilder, fileBuilder);
}
Expand Down
16 changes: 11 additions & 5 deletions src/AsmResolver.DotNet/AssemblyReference.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using AsmResolver.Collections;
using AsmResolver.PE.DotNet.Metadata.Tables;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
Expand Down Expand Up @@ -34,7 +35,7 @@ protected AssemblyReference(MetadataToken token)
/// </summary>
/// <param name="name">The name of the assembly.</param>
/// <param name="version">The version of the assembly.</param>
public AssemblyReference(string? name, Version version)
public AssemblyReference(Utf8String? name, Version version)
: this(new MetadataToken(TableIndex.AssemblyRef, 0))
{
Name = name;
Expand All @@ -50,7 +51,7 @@ public AssemblyReference(string? name, Version version)
/// unhashed public key used to verify the authenticity of the assembly.</param>
/// <param name="publicKeyOrToken">Indicates the public key or token (depending on <paramref name="publicKey"/>),
/// used to verify the authenticity of the assembly.</param>
public AssemblyReference(string? name, Version version, bool publicKey, byte[]? publicKeyOrToken)
public AssemblyReference(Utf8String? name, Version version, bool publicKey, byte[]? publicKeyOrToken)
: this(new MetadataToken(TableIndex.AssemblyRef, 0))
{
Name = name;
Expand Down Expand Up @@ -128,9 +129,14 @@ public byte[]? HashValue
if (!HasPublicKey)
return PublicKeyOrToken;

_publicKeyToken ??= PublicKeyOrToken != null
? ComputePublicKeyToken(PublicKeyOrToken, Resolve()?.HashAlgorithm ?? AssemblyHashAlgorithm.Sha1)
: null;
if (_publicKeyToken is null && PublicKeyOrToken is not null)
{
lock (_publicKeyOrToken)
{
if (_publicKeyToken is null && PublicKeyOrToken is not null)
_publicKeyToken = ComputePublicKeyToken(PublicKeyOrToken, AssemblyHashAlgorithm.Sha1);
}
}

return _publicKeyToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AsmResolver.DotNet.Builder.Metadata.Strings
{
public StringsStreamBlob(Utf8String blob, bool isFixed)
{
Blob = blob.GetBytesUnsafe();
Blob = blob;
Flags = isFixed
? StringsStreamBlobFlags.ZeroTerminated | StringsStreamBlobFlags.Fixed
: StringsStreamBlobFlags.ZeroTerminated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ public class SortedMetadataTableBuffer<TKey, TRow> : ISortedMetadataTableBuffer<
where TKey : notnull
where TRow : struct, IMetadataRow
{
private readonly List<(TKey Key, TRow Row)> _entries = new();
/// <summary>
/// The entries that this table will contain.
/// - Key: The original key to be able to assign metadata tokens easily after sorting.
/// - Row: The metadata row that was constructed for this key.
/// - InputIndex: An index to ensure a stable sort.
/// </summary>
private readonly List<(TKey Key, TRow Row, int InputIndex)> _entries = new();

private readonly Dictionary<TKey, MetadataToken> _newTokens = new();
private readonly MetadataTable<TRow> _table;
private readonly EntryComparer _comparer;
Expand Down Expand Up @@ -48,7 +55,7 @@ public SortedMetadataTableBuffer(MetadataTable<TRow> table, int primaryColumn, i
/// <inheritdoc />
public void Add(TKey originalKey, in TRow row)
{
_entries.Add((originalKey, row));
_entries.Add((originalKey, row, _entries.Count));
}

/// <inheritdoc />
Expand All @@ -61,7 +68,7 @@ public void Sort()

for (uint rid = 1; rid <= _entries.Count; rid++)
{
var (member, _) = _entries[(int) (rid - 1)];
var member = _entries[(int) (rid - 1)].Key;
_newTokens[member] = new MetadataToken(_table.TableIndex, rid);
}
}
Expand All @@ -73,6 +80,7 @@ public void Sort()
public void FlushToTable()
{
Sort();

_table.Clear();
foreach (var row in _entries)
_table.Add(row.Row);
Expand All @@ -85,7 +93,7 @@ public void Clear()
_table.Clear();
}

private sealed class EntryComparer : IComparer<(TKey Key, TRow Row)>
private sealed class EntryComparer : IComparer<(TKey Key, TRow Row, int InputIndex)>
{
private readonly int _primaryColumn;
private readonly int _secondaryColumn;
Expand All @@ -96,11 +104,13 @@ public EntryComparer(int primaryColumn, int secondaryColumn)
_secondaryColumn = secondaryColumn;
}

public int Compare((TKey Key, TRow Row) x, (TKey Key, TRow Row) y)
public int Compare((TKey Key, TRow Row, int InputIndex) x, (TKey Key, TRow Row, int InputIndex) y)
{
int result = x.Row[_primaryColumn].CompareTo(y.Row[_primaryColumn]);
if (result == 0)
result = x.Row[_secondaryColumn].CompareTo(y.Row[_secondaryColumn]);
if (result == 0)
result = x.InputIndex.CompareTo(y.InputIndex);
return result;
}
}
Expand Down
32 changes: 18 additions & 14 deletions src/AsmResolver.DotNet/DefaultMetadataResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public IAssemblyResolver AssemblyResolver
return null;

var resolvedType = LookupInCache(exportedType);
if (resolvedType != null)
if (resolvedType is not null)
return resolvedType;

var resolution = new TypeResolution(AssemblyResolver);
resolvedType = resolution.ResolveExportedType(exportedType);
if (resolvedType != null)
if (resolvedType is not null)
_typeCache[exportedType] = resolvedType;

return resolvedType;
Expand Down Expand Up @@ -137,6 +137,10 @@ public IAssemblyResolver AssemblyResolver
if (declaringType is null)
return null;

var name = field is MemberReference member
? member.Name
: (Utf8String?) field.Name;

for (int i = 0; i < declaringType.Fields.Count; i++)
{
var candidate = declaringType.Fields[i];
Expand Down Expand Up @@ -171,7 +175,7 @@ public TypeResolution(IAssemblyResolver resolver)
{
case TableIndex.AssemblyRef:
var assemblyDefScope = _assemblyResolver.Resolve((AssemblyReference) scope);
return assemblyDefScope != null
return assemblyDefScope is not null
? FindTypeInAssembly(assemblyDefScope, reference.Namespace, reference.Name)
: null;

Expand All @@ -180,7 +184,7 @@ public TypeResolution(IAssemblyResolver resolver)

case TableIndex.TypeRef:
var typeDefScope = ResolveTypeReference((TypeReference) scope);
return typeDefScope != null
return typeDefScope is not null
? FindTypeInType(typeDefScope, reference.Name)
: null;

Expand All @@ -200,20 +204,20 @@ public TypeResolution(IAssemblyResolver resolver)
{
case TableIndex.AssemblyRef:
var assembly = _assemblyResolver.Resolve((AssemblyReference) implementation);
return assembly is {}
return assembly is not null
? FindTypeInAssembly(assembly, exportedType.Namespace, exportedType.Name)
: null;

case TableIndex.File when !string.IsNullOrEmpty(implementation.Name):
var module = FindModuleInAssembly(exportedType.Module!.Assembly!, implementation.Name!);
return module is {}
return module is not null
? FindTypeInModule(module, exportedType.Namespace, exportedType.Name)
: null;

case TableIndex.ExportedType:
var exportedDeclaringType = (ExportedType) implementation;
var declaringType = ResolveExportedType(exportedDeclaringType);
return declaringType is {}
return declaringType is not null
? FindTypeInType(declaringType, exportedType.Name)
: null;

Expand All @@ -222,39 +226,39 @@ public TypeResolution(IAssemblyResolver resolver)
}
}

private TypeDefinition? FindTypeInAssembly(AssemblyDefinition assembly, string? ns, string name)
private TypeDefinition? FindTypeInAssembly(AssemblyDefinition assembly, Utf8String? ns, Utf8String name)
{
for (int i = 0; i < assembly.Modules.Count; i++)
{
var module = assembly.Modules[i];
var type = FindTypeInModule(module, ns, name);
if (type != null)
if (type is not null)
return type;
}

return null;
}

private TypeDefinition? FindTypeInModule(ModuleDefinition module, string? ns, string name)
private TypeDefinition? FindTypeInModule(ModuleDefinition module, Utf8String? ns, Utf8String name)
{
for (int i = 0; i < module.ExportedTypes.Count; i++)
{
var exportedType = module.ExportedTypes[i];
if (exportedType.IsTypeOf(ns, name))
if (exportedType.IsTypeOfUtf8(ns, name))
return ResolveExportedType(exportedType);
}

for (int i = 0; i < module.TopLevelTypes.Count; i++)
{
var type = module.TopLevelTypes[i];
if (type.IsTypeOf(ns, name))
if (type.IsTypeOfUtf8(ns, name))
return type;
}

return null;
}

private static TypeDefinition? FindTypeInType(TypeDefinition enclosingType, string name)
private static TypeDefinition? FindTypeInType(TypeDefinition enclosingType, Utf8String name)
{
for (int i = 0; i < enclosingType.NestedTypes.Count; i++)
{
Expand All @@ -266,7 +270,7 @@ public TypeResolution(IAssemblyResolver resolver)
return null;
}

private static ModuleDefinition? FindModuleInAssembly(AssemblyDefinition assembly, string name)
private static ModuleDefinition? FindModuleInAssembly(AssemblyDefinition assembly, Utf8String name)
{
for (int i = 0; i < assembly.Modules.Count; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/AsmResolver.DotNet/EventDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected EventDefinition(MetadataToken token)
/// <param name="name">The name of the property.</param>
/// <param name="attributes">The attributes.</param>
/// <param name="eventType">The delegate type of the event.</param>
public EventDefinition(string? name, EventAttributes attributes, ITypeDefOrRef? eventType)
public EventDefinition(Utf8String? name, EventAttributes attributes, ITypeDefOrRef? eventType)
: this(new MetadataToken(TableIndex.Event,0))
{
Name = name;
Expand Down Expand Up @@ -94,7 +94,7 @@ public Utf8String? Name
string? INameProvider.Name => Name;

/// <inheritdoc />
public string FullName => FullNameGenerator.GetEventFullName(Name, DeclaringType, EventType);
public string FullName => MemberNameGenerator.GetEventFullName(this);

/// <summary>
/// Gets or sets the delegate type of the event.
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/ExportedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Utf8String? Namespace
string? ITypeDescriptor.Namespace => Namespace;

/// <inheritdoc />
public string FullName => this.GetTypeFullName();
public string FullName => MemberNameGenerator.GetTypeFullName(this);

/// <inheritdoc />
public ModuleDefinition? Module
Expand Down
6 changes: 3 additions & 3 deletions src/AsmResolver.DotNet/FieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected FieldDefinition(MetadataToken token)
/// <param name="name">The name of the field.</param>
/// <param name="attributes">The attributes.</param>
/// <param name="signature">The signature of the field.</param>
public FieldDefinition(string? name, FieldAttributes attributes, FieldSignature? signature)
public FieldDefinition(Utf8String? name, FieldAttributes attributes, FieldSignature? signature)
: this(new MetadataToken(TableIndex.Field, 0))
{
Name = name;
Expand All @@ -71,7 +71,7 @@ public FieldDefinition(string? name, FieldAttributes attributes, FieldSignature?
/// <param name="name">The name of the field.</param>
/// <param name="attributes">The attributes.</param>
/// <param name="fieldType">The type of values the field contains.</param>
public FieldDefinition(string? name, FieldAttributes attributes, TypeSignature? fieldType)
public FieldDefinition(Utf8String name, FieldAttributes attributes, TypeSignature? fieldType)
: this(new MetadataToken(TableIndex.Field, 0))
{
Name = name;
Expand Down Expand Up @@ -105,7 +105,7 @@ public FieldSignature? Signature
}

/// <inheritdoc />
public string FullName => FullNameGenerator.GetFieldFullName(Name, DeclaringType, Signature);
public string FullName => MemberNameGenerator.GetFieldFullName(this);

/// <summary>
/// Gets or sets the attributes associated to the field.
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/FileReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected FileReference(MetadataToken token)
/// </summary>
/// <param name="name">The name of the file.</param>
/// <param name="attributes">The attributes associated to the reference.</param>
public FileReference(string? name, FileAttributes attributes)
public FileReference(Utf8String? name, FileAttributes attributes)
: this(new MetadataToken(TableIndex.File, 0))
{
Name = name;
Expand Down
Loading

0 comments on commit 72f9b45

Please sign in to comment.