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

.NET 3.5 via MonoMod.Backports #540

Merged
merged 13 commits into from
May 17, 2024
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
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet.Dynamic/DynamicMethodHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static object ResolveDynamicResolver(object dynamicMethodObj)

object? ilGenerator = dynamicMethodObj
.GetType()
.GetRuntimeMethods()
.GetMethods((BindingFlags) (-1))
.First(q => q.Name == "GetILGenerator" && q.GetParameters().Length == 0)
.Invoke(dynamicMethodObj, null);

Expand Down
3 changes: 1 addition & 2 deletions src/AsmResolver.DotNet/AssemblyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Versioning;
using System.Threading;
using AsmResolver.Collections;
using AsmResolver.DotNet.Builder;
Expand Down Expand Up @@ -244,7 +243,7 @@ public virtual bool TryGetTargetFramework(out DotNetRuntimeInfo info)
var ctor = CustomAttributes[i].Constructor;

if (ctor?.DeclaringType is not null
&& ctor.DeclaringType.IsTypeOf("System.Runtime.Versioning", nameof(TargetFrameworkAttribute))
&& ctor.DeclaringType.IsTypeOf("System.Runtime.Versioning", "TargetFrameworkAttribute")
&& CustomAttributes[i].Signature?.FixedArguments[0].Element?.ToString() is { } name
&& DotNetRuntimeInfo.TryParse(name, out info))
{
Expand Down
3 changes: 2 additions & 1 deletion src/AsmResolver.DotNet/AssemblyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using AsmResolver.Collections;
using AsmResolver.PE.DotNet.Metadata.Tables;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
using AsmResolver.Shims;
using AssemblyHashAlgorithm = AsmResolver.PE.DotNet.Metadata.Tables.Rows.AssemblyHashAlgorithm;

namespace AsmResolver.DotNet
Expand Down Expand Up @@ -60,7 +61,7 @@ public string FullName

byte[]? publicKeyToken = GetPublicKeyToken();
string publicKeyTokenString = publicKeyToken is not null
? string.Join(string.Empty, publicKeyToken.Select(x => x.ToString("x2")))
? StringShim.Join(string.Empty, publicKeyToken.Select(x => x.ToString("x2")))
: "null";

return $"{Name}, Version={Version}, Culture={cultureString}, PublicKeyToken={publicKeyTokenString}";
Expand Down
3 changes: 2 additions & 1 deletion src/AsmResolver.DotNet/AssemblyResolverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using AsmResolver.DotNet.Signatures;
using AsmResolver.IO;
using AsmResolver.Shims;

namespace AsmResolver.DotNet
{
Expand Down Expand Up @@ -171,7 +172,7 @@ protected virtual AssemblyDefinition LoadAssemblyFromFile(string path)
// If culture is set, prefer the subdirectory with the culture.
if (!string.IsNullOrEmpty(assembly.Culture))
{
path = Path.Combine(directory, assembly.Culture!, assembly.Name);
path = PathShim.Combine(directory, assembly.Culture!, assembly.Name);
string? result = ProbeFileFromFilePathWithoutExtension(path)
?? ProbeFileFromFilePathWithoutExtension(Path.Combine(path, assembly.Name));
if (result is null)
Expand Down
12 changes: 2 additions & 10 deletions src/AsmResolver.DotNet/Builder/MetadataBuilderException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public MetadataBuilderException(string message)
: base(message)
{
}

/// <summary>
/// Creates a new instance of the <see cref="MetadataBuilderException"/> class with the provided message.
/// </summary>
Expand All @@ -35,13 +35,5 @@ public MetadataBuilderException(string message, Exception inner)
: base(message, inner)
{
}

/// <inheritdoc />
protected MetadataBuilderException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
}
}
}
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Bundles/BundleFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void Compress()
using var inputStream = new MemoryStream(GetData());

using var outputStream = new MemoryStream();
using (var deflate = new DeflateStream(outputStream, CompressionLevel.Optimal))
using (var deflate = new DeflateStream(outputStream, CompressionMode.Compress))
{
inputStream.CopyTo(deflate);
}
Expand Down
3 changes: 2 additions & 1 deletion src/AsmResolver.DotNet/Bundles/BundleManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using AsmResolver.PE.File.Headers;
using AsmResolver.PE.Win32Resources;
using AsmResolver.PE.Win32Resources.Builder;
using AsmResolver.Shims;

namespace AsmResolver.DotNet.Bundles
{
Expand Down Expand Up @@ -279,7 +280,7 @@ public string GenerateDeterministicBundleID()
manifestHasher.TransformBlock(fileHash, 0, fileHash.Length, fileHash, 0);
}

manifestHasher.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
manifestHasher.TransformFinalBlock(ArrayShim.Empty<byte>(), 0, 0);
byte[] manifestHash = manifestHasher.Hash!;

return Convert.ToBase64String(manifestHash)
Expand Down
26 changes: 4 additions & 22 deletions src/AsmResolver.DotNet/Code/Cil/CilInstructionCollection.Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public partial class CilInstructionCollection
/// <param name="code">The code.</param>
/// <returns>The created instruction.</returns>
/// <exception cref="InvalidCilInstructionException">Occurs when the provided operation requires an operand.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code) => Insert(Count, code);

/// <summary>
Expand All @@ -30,7 +29,6 @@ public partial class CilInstructionCollection
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="label"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, ICilLabel label) => Insert(Count, code, label);

/// <summary>
Expand All @@ -45,7 +43,6 @@ public partial class CilInstructionCollection
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="labels"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
Insert(Count, code, (IEnumerable<ICilLabel>) labels);

Expand All @@ -61,7 +58,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="labels"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, IEnumerable<ICilLabel> labels) => Insert(Count, code, labels);

/// <summary>
Expand All @@ -73,7 +69,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="InvalidCilInstructionException">
/// Occurs when the provided operation is not an opcode referencing an integer constant.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, int constant) => Insert(Count, code, constant);

/// <summary>
Expand All @@ -85,7 +80,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="InvalidCilInstructionException">
/// Occurs when the provided operation is not an opcode referencing a 64-bit integer constant.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, long constant) => Insert(Count, code, constant);

/// <summary>
Expand All @@ -97,9 +91,8 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="InvalidCilInstructionException">
/// Occurs when the provided operation is not an opcode referencing a float32 constant.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, float constant) => Insert(Count, code, constant);

/// <summary>
/// Verifies and adds a instruction to the end of the collection that references a float64 constant.
/// </summary>
Expand All @@ -109,7 +102,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="InvalidCilInstructionException">
/// Occurs when the provided operation is not an opcode referencing a float64 constant.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, double constant) => Insert(Count, code, constant);

/// <summary>
Expand All @@ -124,7 +116,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="constant"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, string constant) => Insert(Count, code, constant);

/// <summary>
Expand All @@ -139,7 +130,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="variable"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, CilLocalVariable variable) => Insert(Count, code, variable);

/// <summary>
Expand All @@ -154,7 +144,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="parameter"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, Parameter parameter) => Insert(Count, code, parameter);

/// <summary>
Expand All @@ -169,7 +158,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="field"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, IFieldDescriptor field) => Insert(Count, code, field);

/// <summary>
Expand All @@ -184,7 +172,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="method"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, IMethodDescriptor method) => Insert(Count, code, method);

/// <summary>
Expand All @@ -199,7 +186,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="member"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, MemberReference member) => Insert(Count, code, member);

/// <summary>
Expand All @@ -214,7 +200,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="type"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, ITypeDefOrRef type) => Insert(Count, code, type);

/// <summary>
Expand All @@ -229,9 +214,8 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentNullException">
/// Occurs when <paramref name="member"/> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, IMetadataMember member) => Insert(Count, code, member);

/// <summary>
/// Verifies and adds a instruction to the end of the collection that references a standalone signature.
/// </summary>
Expand All @@ -241,9 +225,8 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="InvalidCilInstructionException">
/// Occurs when the provided operation is not an opcode referencing a standalone signature.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, StandAloneSignature signature) => Insert(Count, code, signature);

/// <summary>
/// Verifies and adds a instruction to the end of the collection that references a metadata member by its token.
/// </summary>
Expand All @@ -256,7 +239,6 @@ public CilInstruction Add(CilOpCode code, params ICilLabel[] labels) =>
/// <exception cref="ArgumentOutOfRangeException">
/// Occurs when the provided token is not valid in a CIL stream.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CilInstruction Add(CilOpCode code, MetadataToken token) => Insert(Count, code, token);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace AsmResolver.DotNet.Code.Cil
{
public partial class CilInstructionCollection
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private CilInstruction InsertAndReturn(int index, CilOpCode code, object? operand = null)
{
var instruction = new CilInstruction(code, operand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private static int DetermineVarPushCount(CilInstruction instruction)
/// <param name="variables">The local variables defined in the enclosing method body.</param>
/// <returns>The variable.</returns>
/// <exception cref="ArgumentException">Occurs when the instruction is not using a variant of the ldloc or stloc opcodes.</exception>
public static CilLocalVariable GetLocalVariable(this CilInstruction instruction, IReadOnlyList<CilLocalVariable> variables)
public static CilLocalVariable GetLocalVariable(this CilInstruction instruction, IList<CilLocalVariable> variables)
{
switch (instruction.OpCode.Code)
{
Expand Down
3 changes: 0 additions & 3 deletions src/AsmResolver.DotNet/Code/Cil/CilLabelVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using AsmResolver.PE.DotNet.Cil;

namespace AsmResolver.DotNet.Code.Cil
Expand Down Expand Up @@ -118,11 +117,9 @@ private void VerifyHandlerLabel(int handlerIndex, string labelName, ICilLabel? l
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsPresentInBody(CilInstruction instruction) =>
ReferenceEquals(_body.Instructions.GetByOffset(instruction.Offset), instruction);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsPresentInBody(int offset) =>
_body.Instructions.GetIndexByOffset(offset) >= 0 || _body.Instructions.EndLabel.Offset == offset;

Expand Down
3 changes: 0 additions & 3 deletions src/AsmResolver.DotNet/Code/Cil/CilMaxStackCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,19 @@ private void ScheduleSuccessors(in StackState currentState)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ScheduleLabel(int currentIndex, ICilLabel label, int nextStackSize)
{
int nextIndex = _body.Instructions.GetIndexByOffset(label.Offset);
ScheduleIndex(currentIndex, nextIndex, label.Offset, nextStackSize);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ScheduleDelta(int currentIndex, int offsetDelta, int nextStackSize)
{
int nextOffset = _body.Instructions[currentIndex].Offset + offsetDelta;
int nextIndex = _body.Instructions.GetIndexByOffset(nextOffset);
ScheduleIndex(currentIndex, nextIndex, nextOffset, nextStackSize);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ScheduleNext(int currentIndex, int nextStackSize)
{
var instruction = _body.Instructions[currentIndex];
Expand Down
2 changes: 0 additions & 2 deletions src/AsmResolver.DotNet/Code/Cil/CilMethodBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ private static void ReadExceptionHandlers(
/// <exception cref="InvalidCilInstructionException">Occurs when one branch instruction in the method body is invalid.</exception>
/// <exception cref="AggregateException">Occurs when multiple branch instructions in the method body are invalid.</exception>
/// <remarks>This method will force the offsets of each instruction to be calculated.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void VerifyLabels() => VerifyLabels(true);

/// <summary>
Expand All @@ -274,7 +273,6 @@ public void VerifyLabels(bool calculateOffsets)
/// </summary>
/// <exception cref="StackImbalanceException">Occurs when the method body will result in an unbalanced stack.</exception>
/// <remarks>This method will force the offsets of each instruction to be calculated.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ComputeMaxStack() => ComputeMaxStack(true);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,5 @@ public InvalidCilInstructionException(string message, Exception inner)
: base(message, inner)
{
}

/// <inheritdoc />
protected InvalidCilInstructionException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
}
}
}
4 changes: 3 additions & 1 deletion src/AsmResolver.DotNet/Code/Native/NativeMethodBody.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using AsmResolver.Collections;
using AsmResolver.PE.Code;
using AsmResolver.Shims;

namespace AsmResolver.DotNet.Code.Native
{
Expand All @@ -17,7 +19,7 @@ public class NativeMethodBody : MethodBody
public NativeMethodBody(MethodDefinition owner)
: base(owner)
{
Code = Array.Empty<byte>();
Code = ArrayShim.Empty<byte>();
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/AsmResolver.DotNet/Code/Native/NativeSymbolsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using AsmResolver.Collections;
using AsmResolver.PE.Exports;
using AsmResolver.PE.Imports;
using AsmResolver.PE.Relocations;
using AsmResolver.Shims;

namespace AsmResolver.DotNet.Code.Native
{
Expand Down Expand Up @@ -150,7 +152,7 @@ public IEnumerable<ExportedSymbol> GetExportedSymbols(out uint baseOrdinal)
{
baseOrdinal = 1;
if (_fixedExportedSymbols.Count == 0 && _floatingExportedSymbols.Count == 0)
return Array.Empty<ExportedSymbol>();
return ArrayShim.Empty<ExportedSymbol>();

// Check if no fixed symbols at all, then just return the floating exports.
if (_fixedExportedSymbols.Count == 0)
Expand Down
Loading