9 Commits
Author SHA1 Message Date
ds5678 58e24a2bb2 Bump version 2024-06-15 23:27:34 -07:00
ds5678 88e6e617cf Add more extension methods 2024-06-15 23:26:17 -07:00
ds5678 92dbe51e75 public MethodDefinitionExtensions 2024-06-15 20:45:24 -07:00
ds5678 0f996365c6 Make CilInstructionCollectionExtensions public 2024-06-15 20:14:18 -07:00
ds5678 5f3fbe86fe Update to AsmResolver 6 2024-06-15 20:06:14 -07:00
ds5678 b49a6b2a75 Bump version 2024-04-14 15:03:55 -07:00
ds5678 ff8d07d1b4 Initialize struct fields 2024-04-14 15:02:13 -07:00
ds5678 a7d7cc27fb Fix static properties and bump version 2024-01-14 14:02:50 -05:00
ds5678 7c897a399f Simplify code for generating the base constructor call 2023-12-31 15:56:22 -05:00
8 changed files with 223 additions and 66 deletions

No files matched your search

+2 -2
View File
@@ -15,12 +15,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AsmResolver.DotNet" Version="5.5.0" />
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.1" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="/"/>
<None Include="../README.md" Pack="true" PackagePath="/" />
</ItemGroup>
</Project>
@@ -1,13 +1,13 @@
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.DotNet.Signatures;
using AsmResolver.PE.DotNet.Cil;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
using AsmResolver.PE.DotNet.Metadata.Tables;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace AssetRipper.CIL;
internal static class CilInstructionCollectionExtensions
public static class CilInstructionCollectionExtensions
{
public static CilLocalVariable AddLocalVariable(this CilInstructionCollection instructions, TypeSignature variableType)
{
@@ -16,7 +16,7 @@ internal static class CilInstructionCollectionExtensions
return variable;
}
public static void AddDefaultValueForType(this CilInstructionCollection instructions, TypeSignature type)
public static void AddDefaultValue(this CilInstructionCollection instructions, TypeSignature type)
{
if (type is CorLibTypeSignature { IsValueType: true } corLibTypeSignature)
{
@@ -36,6 +36,60 @@ internal static class CilInstructionCollectionExtensions
}
}
/// <summary>
/// Remove the last instruction in the collection
/// </summary>
/// <param name="instructions">The collection to remove the instruction from</param>
public static void Pop(this CilInstructionCollection instructions) => instructions.RemoveAt(instructions.Count - 1);
public static CilInstruction AddLoadElement(this CilInstructionCollection instructions, TypeSignature elementType)
{
return elementType switch
{
CorLibTypeSignature corLibTypeSignature => corLibTypeSignature.ElementType switch
{
ElementType.Boolean => instructions.Add(CilOpCodes.Ldelem_U1),
ElementType.I1 => instructions.Add(CilOpCodes.Ldelem_I1),
ElementType.U1 => instructions.Add(CilOpCodes.Ldelem_U1),
ElementType.I2 => instructions.Add(CilOpCodes.Ldelem_I2),
ElementType.U2 => instructions.Add(CilOpCodes.Ldelem_U2),
ElementType.I4 => instructions.Add(CilOpCodes.Ldelem_I4),
ElementType.U4 => instructions.Add(CilOpCodes.Ldelem_U4),
ElementType.I8 => instructions.Add(CilOpCodes.Ldelem_I8),
ElementType.U8 => instructions.Add(CilOpCodes.Ldelem_I8),
ElementType.R4 => instructions.Add(CilOpCodes.Ldelem_R4),
ElementType.R8 => instructions.Add(CilOpCodes.Ldelem_R8),
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Ldelem) : instructions.Add(CilOpCodes.Ldelem_Ref)
},
GenericParameterSignature => instructions.Add(CilOpCodes.Ldelem),
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Ldelem) : instructions.Add(CilOpCodes.Ldelem_Ref)
};
}
public static CilInstruction AddStoreElement(this CilInstructionCollection instructions, TypeSignature elementType)
{
return elementType switch
{
CorLibTypeSignature corLibTypeSignature => corLibTypeSignature.ElementType switch
{
ElementType.Boolean => instructions.Add(CilOpCodes.Stelem_I1),
ElementType.I1 => instructions.Add(CilOpCodes.Stelem_I1),
ElementType.U1 => instructions.Add(CilOpCodes.Stelem_I1),
ElementType.I2 => instructions.Add(CilOpCodes.Stelem_I2),
ElementType.U2 => instructions.Add(CilOpCodes.Stelem_I2),
ElementType.I4 => instructions.Add(CilOpCodes.Stelem_I4),
ElementType.U4 => instructions.Add(CilOpCodes.Stelem_I4),
ElementType.I8 => instructions.Add(CilOpCodes.Stelem_I8),
ElementType.U8 => instructions.Add(CilOpCodes.Stelem_I8),
ElementType.R4 => instructions.Add(CilOpCodes.Stelem_R4),
ElementType.R8 => instructions.Add(CilOpCodes.Stelem_R8),
_ => instructions.Add(CilOpCodes.Stelem_Ref),
},
GenericParameterSignature => instructions.Add(CilOpCodes.Stelem),
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Stelem) : instructions.Add(CilOpCodes.Stelem_Ref)
};
}
/// <summary>
/// Load a null reference onto the stack.
/// </summary>
@@ -46,7 +100,7 @@ internal static class CilInstructionCollectionExtensions
/// However, it does produce a compiler warning.
/// </remarks>
/// <param name="instructions"></param>
private static void AddNullRef(this CilInstructionCollection instructions)
public static void AddNullRef(this CilInstructionCollection instructions)
{
instructions.Add(CilOpCodes.Ldc_I4_0);
instructions.Add(CilOpCodes.Conv_U);
+61 -7
View File
@@ -1,21 +1,49 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Collections;
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
using AsmResolver.PE.DotNet.Metadata.Tables;
using System.Diagnostics.CodeAnalysis;
namespace AssetRipper.CIL;
internal static class MethodDefinitionExtensions
public static class MethodDefinitionExtensions
{
/// <summary>
/// Replaces the method body with a sensible minimal implementation.
/// If the method is for an auto-property, the method will be replaced with a simple getter or setter.
/// Otherwise, the method content will be filled with a valid default implementation that does not throw
/// and handles all required logic, such as setting out parameters.
/// </summary>
/// <remarks>
/// If the method is not managed or should not have a body, this method does nothing.
/// </remarks>
/// <param name="methodDefinition">The method to modify.</param>
public static void ReplaceMethodBodyWithMinimalImplementation(this MethodDefinition methodDefinition)
{
MethodStubber.FillMethodBodyWithStub(methodDefinition);
}
public static bool IsInstanceConstructor(this MethodDefinition methodDefinition)
{
return methodDefinition.IsConstructor && !methodDefinition.IsStatic;
}
public static bool ShouldCallBaseConstructor(this MethodDefinition methodDefinition)
internal static bool ShouldCallBaseConstructor(this MethodDefinition methodDefinition)
{
return methodDefinition.IsInstanceConstructor() && methodDefinition.DeclaringType?.IsValueType is not true;
return methodDefinition.IsInstanceConstructor() && !methodDefinition.IsDeclaredByValueType();
}
/// <summary>
/// Is this method declared inside a struct?
/// </summary>
public static bool IsDeclaredByValueType(this MethodDefinition methodDefinition)
{
return methodDefinition.DeclaringType?.IsValueType is true;
}
internal static bool ShouldInitializeFields(this MethodDefinition methodDefinition)
{
return methodDefinition.IsInstanceConstructor() && methodDefinition.IsDeclaredByValueType();
}
public static bool IsManagedMethodWithBody(this MethodDefinition managedMethod)
@@ -42,7 +70,9 @@ internal static class MethodDefinitionExtensions
string backingFieldName = $"<{method.Name.Value[4..]}>k__BackingField";
FieldDefinition? field = method.DeclaringType.Fields.FirstOrDefault(f => f.Name == backingFieldName);
if (field is null || !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType))
if (field is null
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType)
|| field.IsStatic != method.IsStatic)
{
backingField = null;
return false;
@@ -69,7 +99,9 @@ internal static class MethodDefinitionExtensions
string backingFieldName = $"<{method.Name.Value[4..]}>k__BackingField";
FieldDefinition? field = method.DeclaringType.Fields.FirstOrDefault(f => f.Name == backingFieldName);
if (field is null || !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType))
if (field is null
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType)
|| field.IsStatic != method.IsStatic)
{
backingField = null;
return false;
@@ -80,4 +112,26 @@ internal static class MethodDefinitionExtensions
return true;
}
}
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName)
{
return method.AddParameter(parameterSignature, parameterName, out _);
}
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName, out ParameterDefinition parameterDefinition)
{
parameterDefinition = new ParameterDefinition((ushort)(method.Signature!.ParameterTypes.Count + 1), parameterName, default);
method.Signature.ParameterTypes.Add(parameterSignature);
method.ParameterDefinitions.Add(parameterDefinition);
method.Parameters.PullUpdatesFromMethodSignature();
return method.Parameters.Single(parameter => parameter.Name == parameterName && parameter.ParameterType == parameterSignature);
}
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature)
{
method.Signature!.ParameterTypes.Add(parameterSignature);
method.Parameters.PullUpdatesFromMethodSignature();
return method.Parameters[^1];
}
}
+44 -24
View File
@@ -2,15 +2,14 @@
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Collections;
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.PE.DotNet.Cil;
using System.Diagnostics.CodeAnalysis;
namespace AssetRipper.CIL;
public static class MethodStubber
internal static class MethodStubber
{
public static void FillMethodBodyWithStub(this MethodDefinition methodDefinition)
public static void FillMethodBodyWithStub(MethodDefinition methodDefinition)
{
if (!methodDefinition.IsManagedMethodWithBody())
{
@@ -22,42 +21,63 @@ public static class MethodStubber
if (methodDefinition.IsAutoPropertyGetMethod(out FieldDefinition? backingField))
{
methodInstructions.Add(CilOpCodes.Ldarg_0);
methodInstructions.Add(CilOpCodes.Ldfld, backingField);
if (methodDefinition.IsStatic)
{
methodInstructions.Add(CilOpCodes.Ldsfld, backingField);
}
else
{
methodInstructions.Add(CilOpCodes.Ldarg_0);
methodInstructions.Add(CilOpCodes.Ldfld, backingField);
}
methodInstructions.Add(CilOpCodes.Ret);
return;
}
else if (methodDefinition.IsAutoPropertySetMethod(out backingField))
{
methodInstructions.Add(CilOpCodes.Ldarg_0);
methodInstructions.Add(CilOpCodes.Ldarg_1);
methodInstructions.Add(CilOpCodes.Stfld, backingField);
if (methodDefinition.IsStatic)
{
methodInstructions.Add(CilOpCodes.Ldarg_0);
methodInstructions.Add(CilOpCodes.Stsfld, backingField);
}
else
{
methodInstructions.Add(CilOpCodes.Ldarg_0);
methodInstructions.Add(CilOpCodes.Ldarg_1);
methodInstructions.Add(CilOpCodes.Stfld, backingField);
}
methodInstructions.Add(CilOpCodes.Ret);
return;
}
if (methodDefinition.ShouldInitializeFields())
{
TypeDefinition declaringType = methodDefinition.DeclaringType!;
foreach (FieldDefinition field in declaringType.Fields)
{
if (field.IsStatic || field.Signature is null)
{
continue;
}
methodInstructions.Add(CilOpCodes.Ldarg_0);
methodInstructions.AddDefaultValue(field.Signature.FieldType);
methodInstructions.Add(CilOpCodes.Stfld, field);
}
}
if (methodDefinition.ShouldCallBaseConstructor() && TryGetBaseConstructor(methodDefinition, out IMethodDescriptor? baseConstructor))
{
if (baseConstructor is MethodDefinition baseConstructorDef)
MethodSignature? methodSignature = baseConstructor.Signature;
if (methodSignature is not null)
{
methodInstructions.Add(CilOpCodes.Ldarg_0);
foreach (Parameter baseParameter in baseConstructorDef.Parameters)
foreach (TypeSignature baseParameterType in methodSignature.ParameterTypes)
{
TypeSignature importedBaseParameterType = methodDefinition.DeclaringType!.Module!.DefaultImporter.ImportTypeSignature(baseParameter.ParameterType);
methodInstructions.AddDefaultValueForType(importedBaseParameterType);
methodInstructions.AddDefaultValue(baseParameterType);
}
methodInstructions.Add(CilOpCodes.Call, baseConstructorDef);
methodInstructions.Add(CilOpCodes.Call, baseConstructor);
}
else if (baseConstructor is MemberReference baseConstructorRef)
{
methodInstructions.Add(CilOpCodes.Ldarg_0);
foreach (TypeSignature baseParameterType in ((MethodSignature)baseConstructorRef.Signature!).ParameterTypes)
{
methodInstructions.AddDefaultValueForType(baseParameterType);
}
methodInstructions.Add(CilOpCodes.Call, baseConstructorRef);
}
//Constructors cannot be MethodSpecifications, so no need to handle that case.
}
foreach (Parameter parameter in methodDefinition.Parameters)
@@ -82,7 +102,7 @@ public static class MethodStubber
}
}
}
methodInstructions.AddDefaultValueForType(methodDefinition.Signature!.ReturnType);
methodInstructions.AddDefaultValue(methodDefinition.Signature!.ReturnType);
methodInstructions.Add(CilOpCodes.Ret);
methodInstructions.OptimizeMacros();
}
+1 -1
View File
@@ -1,5 +1,5 @@
using AsmResolver.DotNet.Collections;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.DotNet.Signatures;
using System.Diagnostics.CodeAnalysis;
namespace AssetRipper.CIL;
@@ -0,0 +1,29 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Signatures;
using AsmResolver.PE.DotNet.Metadata.Tables;
namespace AssetRipper.CIL;
public static class TypeDefinitionExtensions
{
public static MethodDefinition AddMethod(this TypeDefinition type, string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
{
MethodDefinition method = CreateMethod(methodName, methodAttributes, returnType);
type.Methods.Add(method);
return method;
}
private static MethodDefinition CreateMethod(string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
{
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
MethodSignature methodSignature = isStatic
? MethodSignature.CreateStatic(returnType)
: MethodSignature.CreateInstance(returnType);
MethodDefinition result = new MethodDefinition(methodName, methodAttributes, methodSignature);
result.CilMethodBody = new CilMethodBody(result);
return result;
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.DotNet.Signatures;
namespace AssetRipper.CIL;
+26 -26
View File
@@ -1,27 +1,27 @@
<Project>
<PropertyGroup>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<IsAotCompatible>true</IsAotCompatible>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
<Version>1.0.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Copyright>Copyright (c) 2024 ds5678</Copyright>
<Authors>ds5678</Authors>
<Company>AssetRipper</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/AssetRipper/AssetRipper.CIL</RepositoryUrl>
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
<Project>
<PropertyGroup>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<IsAotCompatible>true</IsAotCompatible>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
<Version>1.1.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Copyright>Copyright (c) 2024 ds5678</Copyright>
<Authors>ds5678</Authors>
<Company>AssetRipper</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/AssetRipper/AssetRipper.CIL</RepositoryUrl>
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
</Project>