mirror of
https://github.com/AssetRipper/AssetRipper.CIL.git
synced 2026-11-13 12:41:25 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58e24a2bb2 | ||
|
|
88e6e617cf | ||
|
|
92dbe51e75 | ||
|
|
0f996365c6 | ||
|
|
5f3fbe86fe |
No files matched your search
@@ -15,7 +15,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="5.5.1" />
|
||||
<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>
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,19 +1,34 @@
|
||||
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.IsDeclaredByValueType();
|
||||
}
|
||||
@@ -26,7 +41,7 @@ internal static class MethodDefinitionExtensions
|
||||
return methodDefinition.DeclaringType?.IsValueType is true;
|
||||
}
|
||||
|
||||
public static bool ShouldInitializeFields(this MethodDefinition methodDefinition)
|
||||
internal static bool ShouldInitializeFields(this MethodDefinition methodDefinition)
|
||||
{
|
||||
return methodDefinition.IsInstanceConstructor() && methodDefinition.IsDeclaredByValueType();
|
||||
}
|
||||
@@ -97,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];
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
{
|
||||
@@ -62,7 +61,7 @@ public static class MethodStubber
|
||||
}
|
||||
|
||||
methodInstructions.Add(CilOpCodes.Ldarg_0);
|
||||
methodInstructions.AddDefaultValueForType(field.Signature.FieldType);
|
||||
methodInstructions.AddDefaultValue(field.Signature.FieldType);
|
||||
methodInstructions.Add(CilOpCodes.Stfld, field);
|
||||
}
|
||||
}
|
||||
@@ -75,7 +74,7 @@ public static class MethodStubber
|
||||
methodInstructions.Add(CilOpCodes.Ldarg_0);
|
||||
foreach (TypeSignature baseParameterType in methodSignature.ParameterTypes)
|
||||
{
|
||||
methodInstructions.AddDefaultValueForType(baseParameterType);
|
||||
methodInstructions.AddDefaultValue(baseParameterType);
|
||||
}
|
||||
methodInstructions.Add(CilOpCodes.Call, baseConstructor);
|
||||
}
|
||||
@@ -103,7 +102,7 @@ public static class MethodStubber
|
||||
}
|
||||
}
|
||||
}
|
||||
methodInstructions.AddDefaultValueForType(methodDefinition.Signature!.ReturnType);
|
||||
methodInstructions.AddDefaultValue(methodDefinition.Signature!.ReturnType);
|
||||
methodInstructions.Add(CilOpCodes.Ret);
|
||||
methodInstructions.OptimizeMacros();
|
||||
}
|
||||
|
||||
@@ -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,4 +1,4 @@
|
||||
using AsmResolver.DotNet.Signatures.Types;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
|
||||
|
||||
<Version>1.0.2</Version>
|
||||
<Version>1.1.0</Version>
|
||||
<AssemblyVersion>$(Version)</AssemblyVersion>
|
||||
<Copyright>Copyright (c) 2024 ds5678</Copyright>
|
||||
<Authors>ds5678</Authors>
|
||||
|
||||
Reference in new issue
Block a user