mirror of
https://github.com/AssetRipper/AssetRipper.CIL.git
synced 2026-11-13 04:26:24 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d68e67393 | ||
|
|
d8230781d9 | ||
|
|
2990c647c5 | ||
|
|
e871881517 | ||
|
|
c1eceeda51 | ||
|
|
44996e27ff | ||
|
|
0039dc95b3 | ||
|
|
0953640bf1 | ||
|
|
c1e8657d67 |
No files matched your search
@@ -13,13 +13,13 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="NUnit" Version="4.3.2" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.6.0">
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="NUnit" Version="4.4.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.10.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -94,7 +94,7 @@ public class LoadIndirectTests
|
||||
private static void AssertCorrectLoadIndirect(TypeSignature typeToLoad, CilCode expectedOpCode)
|
||||
{
|
||||
MethodDefinition method = new MethodDefinition("Test", MethodAttributes.Public | MethodAttributes.Static, null);
|
||||
method.CilMethodBody = new(method);
|
||||
method.CilMethodBody = new();
|
||||
CilInstructionCollection instructions = method.CilMethodBody.Instructions;
|
||||
instructions.AddLoadIndirect(typeToLoad);
|
||||
Assert.That(instructions, Has.Count.EqualTo(1));
|
||||
|
||||
@@ -94,7 +94,7 @@ public class StoreIndirectTests
|
||||
private static void AssertCorrectStoreIndirect(TypeSignature typeToStore, CilCode expectedOpCode)
|
||||
{
|
||||
MethodDefinition method = new MethodDefinition("Test", MethodAttributes.Public | MethodAttributes.Static, null);
|
||||
method.CilMethodBody = new(method);
|
||||
method.CilMethodBody = new();
|
||||
CilInstructionCollection instructions = method.CilMethodBody.Instructions;
|
||||
instructions.AddStoreIndirect(typeToStore);
|
||||
Assert.That(instructions, Has.Count.EqualTo(1));
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.3" />
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.4" />
|
||||
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.1" />
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -71,6 +71,12 @@ public static class CilInstructionCollectionExtensions
|
||||
instructions.Add(CilOpCodes.Ceq);
|
||||
}
|
||||
|
||||
public static void AddThrowNull(this CilInstructionCollection instructions)
|
||||
{
|
||||
instructions.Add(CilOpCodes.Ldnull);
|
||||
instructions.Add(CilOpCodes.Throw);
|
||||
}
|
||||
|
||||
public static CilInstruction AddLoadElement(this CilInstructionCollection instructions, TypeSignature elementType)
|
||||
{
|
||||
return elementType switch
|
||||
|
||||
@@ -12,4 +12,19 @@ public static class CustomAttributeExtensions
|
||||
attribute.Signature.FixedArguments.Add(argument);
|
||||
return argument;
|
||||
}
|
||||
|
||||
public static CustomAttributeArgument AddFixedArgument<T>(this CustomAttribute attribute, TypeSignature paramType, T[] paramValue)
|
||||
{
|
||||
CustomAttributeArgument argument = new CustomAttributeArgument(paramType, paramValue.Select(e => (object?)e));
|
||||
attribute.Signature!.FixedArguments.Add(argument);
|
||||
return argument;
|
||||
}
|
||||
|
||||
public static CustomAttributeNamedArgument AddNamedArgument(this CustomAttribute attribute, TypeSignature memberType, string memberName, TypeSignature paramType, object paramValue, CustomAttributeArgumentMemberType memberKind)
|
||||
{
|
||||
CustomAttributeArgument argument = new CustomAttributeArgument(paramType, paramValue);
|
||||
CustomAttributeNamedArgument namedArgument = new CustomAttributeNamedArgument(memberKind, memberName, memberType, argument);
|
||||
attribute.Signature!.NamedArguments.Add(namedArgument);
|
||||
return namedArgument;
|
||||
}
|
||||
}
|
||||
@@ -27,9 +27,26 @@ public static class IHasCustomAttributeExtensions
|
||||
return attribute;
|
||||
}
|
||||
|
||||
public static CustomAttribute AddCustomAttribute<T>(this IHasCustomAttribute _this, IMethodDefOrRef constructor, SzArrayTypeSignature paramType, T[] paramValue)
|
||||
{
|
||||
CustomAttribute attribute = _this.AddCustomAttribute(constructor);
|
||||
attribute.AddFixedArgument(paramType, paramValue);
|
||||
return attribute;
|
||||
}
|
||||
|
||||
public static CustomAttribute AddCustomAttribute(this IHasCustomAttribute _this, IMethodDefOrRef constructor, params (TypeSignature, object)[] parameters)
|
||||
{
|
||||
CustomAttribute attribute = _this.AddCustomAttribute(constructor);
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
attribute.AddFixedArgument(parameters[i].Item1, parameters[i].Item2);
|
||||
}
|
||||
return attribute;
|
||||
}
|
||||
|
||||
internal static ModuleDefinition GetModule(this IHasCustomAttribute _this)
|
||||
{
|
||||
return (_this as IModuleProvider)?.Module ?? throw new ArgumentException("Entity does not have a module", nameof(_this));
|
||||
return (_this as IModuleProvider)?.ContextModule ?? throw new ArgumentException("Entity does not have a module", nameof(_this));
|
||||
}
|
||||
|
||||
public static CustomAttribute AddFlagsAttribute(this IHasCustomAttribute _this)
|
||||
@@ -44,7 +61,7 @@ public static class IHasCustomAttributeExtensions
|
||||
{
|
||||
TypeReference flagsTypeRef = new(module.CorLibTypeFactory.CorLibScope, "System", nameof(FlagsAttribute));
|
||||
MemberReference constructor = new(flagsTypeRef, ".ctor", MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));
|
||||
return _this.AddCustomAttribute(constructor);
|
||||
return _this.AddCustomAttribute(module.DefaultImporter.ImportMethod(constructor));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +86,7 @@ public static class IHasCustomAttributeExtensions
|
||||
|
||||
MemberReference constructor = new(compilerGeneratedTypeRef, ".ctor", MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));
|
||||
|
||||
return _this.AddCustomAttribute(constructor);
|
||||
return _this.AddCustomAttribute(module.DefaultImporter.ImportMethod(constructor));
|
||||
}
|
||||
|
||||
static AssemblyReference GetOrAddReferenceToSystemRuntimeCompilerServices(ModuleDefinition module)
|
||||
|
||||
@@ -11,7 +11,7 @@ public sealed class SwitchRemover : ICilManipulator
|
||||
{
|
||||
public void Execute(CilManipulationContext context)
|
||||
{
|
||||
ModuleDefinition? module = context.Instructions.Owner.Owner.Module;
|
||||
ModuleDefinition? module = context.Instructions.Owner.Owner?.DeclaringModule;
|
||||
if (module is null)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Code.Cil;
|
||||
using AsmResolver.DotNet.Collections;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
using AsmResolver.PE.DotNet.Metadata.Tables;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
@@ -23,6 +25,24 @@ public static class MethodDefinitionExtensions
|
||||
MethodStubber.FillMethodBodyWithStub(methodDefinition);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the method body with: <code>throw null;</code>
|
||||
/// </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 ReplaceMethodBodyWithThrowNull(this MethodDefinition methodDefinition)
|
||||
{
|
||||
if (!methodDefinition.IsManagedMethodWithBody())
|
||||
{
|
||||
return;
|
||||
}
|
||||
methodDefinition.CilMethodBody = new();
|
||||
CilInstructionCollection methodInstructions = methodDefinition.CilMethodBody.Instructions;
|
||||
methodInstructions.AddThrowNull();
|
||||
}
|
||||
|
||||
public static bool IsInstance(this MethodDefinition methodDefinition)
|
||||
{
|
||||
return !methodDefinition.IsStatic;
|
||||
@@ -72,6 +92,11 @@ public static class MethodDefinitionExtensions
|
||||
&& !managedMethod.IsRuntime;
|
||||
}
|
||||
|
||||
public static CilInstructionCollection GetInstructions(this MethodDefinition method)
|
||||
{
|
||||
return method.CilMethodBody?.Instructions ?? throw new ArgumentException("CilMethodBody was null", nameof(method));
|
||||
}
|
||||
|
||||
public static bool IsAutoPropertyGetMethod(this MethodDefinition method, [NotNullWhen(true)] out FieldDefinition? backingField)
|
||||
{
|
||||
if (!method.IsGetMethod
|
||||
@@ -131,12 +156,12 @@ public static class MethodDefinitionExtensions
|
||||
}
|
||||
}
|
||||
|
||||
public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName)
|
||||
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)
|
||||
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);
|
||||
|
||||
@@ -16,7 +16,7 @@ internal static class MethodStubber
|
||||
return;
|
||||
}
|
||||
|
||||
methodDefinition.CilMethodBody = new(methodDefinition);
|
||||
methodDefinition.CilMethodBody = new();
|
||||
CilInstructionCollection methodInstructions = methodDefinition.CilMethodBody.Instructions;
|
||||
|
||||
if (methodDefinition.IsAutoPropertyGetMethod(out FieldDefinition? backingField))
|
||||
@@ -131,13 +131,13 @@ internal static class MethodStubber
|
||||
return null;
|
||||
}
|
||||
MethodDefinition? baseConstructor = GetFirstCompatibleConstructor(methodDefinition.DeclaringType, baseTypeResolved);
|
||||
if (baseConstructor is null || methodDefinition.DeclaringType.Module is null)
|
||||
if (baseConstructor is null || methodDefinition.DeclaringType.DeclaringModule is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return methodDefinition.DeclaringType.Module.DefaultImporter.ImportMethod(baseConstructor);
|
||||
return methodDefinition.DeclaringType.DeclaringModule.DefaultImporter.ImportMethod(baseConstructor);
|
||||
}
|
||||
}
|
||||
else if (baseType is TypeSpecification baseTypeSpec)
|
||||
@@ -153,11 +153,11 @@ internal static class MethodStubber
|
||||
return null;
|
||||
}
|
||||
MethodDefinition? baseConstructor = GetFirstCompatibleConstructor(methodDefinition.DeclaringType, baseTypeResolved);
|
||||
if (baseConstructor is null || methodDefinition.DeclaringType.Module is null)
|
||||
if (baseConstructor is null || methodDefinition.DeclaringType.DeclaringModule is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
IMethodDefOrRef baseConstructorImported = methodDefinition.DeclaringType.Module.DefaultImporter.ImportMethod(baseConstructor);
|
||||
IMethodDefOrRef baseConstructorImported = methodDefinition.DeclaringType.DeclaringModule.DefaultImporter.ImportMethod(baseConstructor);
|
||||
return new MemberReference(baseType, baseConstructorImported.Name, baseConstructorImported.Signature);
|
||||
}
|
||||
|
||||
@@ -165,14 +165,7 @@ internal static class MethodStubber
|
||||
|
||||
static MethodDefinition? GetFirstCompatibleConstructor(TypeDefinition declaringType, TypeDefinition baseType)
|
||||
{
|
||||
if (declaringType.Module == baseType.Module)
|
||||
{
|
||||
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && !m.IsPrivate);
|
||||
}
|
||||
else
|
||||
{
|
||||
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && (m.IsFamily || m.IsPublic));
|
||||
}
|
||||
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && m.IsAccessibleFromType(declaringType));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +183,7 @@ internal static class MethodStubber
|
||||
}
|
||||
|
||||
TypeSignature[] typeArguments = Enumerable.Range(0, field.DeclaringType.GenericParameters.Count)
|
||||
.Select(i => new GenericParameterSignature(field.Module, GenericParameterType.Type, i))
|
||||
.Select(i => new GenericParameterSignature(field.DeclaringModule, GenericParameterType.Type, i))
|
||||
.ToArray();
|
||||
|
||||
return new MemberReference(field.DeclaringType.MakeGenericInstanceType(typeArguments).ToTypeDefOrRef(), field.Name, field.Signature);
|
||||
|
||||
@@ -25,7 +25,7 @@ public static class ModuleDefinitionExtensions
|
||||
?? throw new ArgumentOutOfRangeException(nameof(underlyingType), underlyingType, null);
|
||||
|
||||
TypeReference baseType = new(module.CorLibTypeFactory.CorLibScope, "System", "Enum");
|
||||
TypeDefinition type = new(@namespace, name, visibility.ToTypeAttributes() | TypeAttributes.Sealed, baseType);
|
||||
TypeDefinition type = new(@namespace, name, visibility.ToTypeAttributes() | TypeAttributes.Sealed, module.DefaultImporter.ImportType(baseType));
|
||||
module.TopLevelTypes.Add(type);
|
||||
AddEnumValue(type, underlyingTypeSignature);
|
||||
return type;
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace AssetRipper.CIL;
|
||||
|
||||
public static class TypeDefinitionExtensions
|
||||
{
|
||||
public static FieldDefinition AddField(this TypeDefinition type, string fieldName, TypeSignature fieldType, bool isStatic = false, Visibility visibility = Visibility.Public)
|
||||
public static FieldDefinition AddField(this TypeDefinition type, string? fieldName, TypeSignature fieldType, bool isStatic = false, Visibility visibility = Visibility.Public)
|
||||
{
|
||||
FieldAttributes fieldAttributes = visibility.ToFieldAttributes() | (isStatic ? FieldAttributes.Static : default);
|
||||
FieldDefinition field = new FieldDefinition(fieldName, fieldAttributes, fieldType);
|
||||
@@ -34,7 +34,7 @@ public static class TypeDefinitionExtensions
|
||||
return field;
|
||||
}
|
||||
|
||||
public static MethodDefinition AddMethod(this TypeDefinition type, string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
|
||||
public static MethodDefinition AddMethod(this TypeDefinition type, string? methodName, MethodAttributes methodAttributes, TypeSignature returnType)
|
||||
{
|
||||
MethodDefinition method = CreateMethod(methodName, methodAttributes, returnType);
|
||||
type.Methods.Add(method);
|
||||
@@ -44,7 +44,7 @@ public static class TypeDefinitionExtensions
|
||||
public static MethodDefinition AddConstructor(this TypeDefinition type, Visibility visibility = Visibility.Public)
|
||||
{
|
||||
MethodAttributes methodAttributes = visibility.ToMethodAttributes() | MethodAttributes.SpecialName | MethodAttributes.RuntimeSpecialName;
|
||||
return type.AddMethod(".ctor", methodAttributes, type.Module!.CorLibTypeFactory.Void);
|
||||
return type.AddMethod(".ctor", methodAttributes, type.DeclaringModule!.CorLibTypeFactory.Void);
|
||||
}
|
||||
|
||||
public static FieldDefinition AddEnumField(this TypeDefinition type, string name, long value)
|
||||
@@ -67,7 +67,7 @@ public static class TypeDefinitionExtensions
|
||||
};
|
||||
}
|
||||
|
||||
private static MethodDefinition CreateMethod(string methodName, MethodAttributes methodAttributes, TypeSignature returnType)
|
||||
private static MethodDefinition CreateMethod(string? methodName, MethodAttributes methodAttributes, TypeSignature returnType)
|
||||
{
|
||||
bool isStatic = (methodAttributes & MethodAttributes.Static) != 0;
|
||||
MethodSignature methodSignature = isStatic
|
||||
@@ -75,17 +75,30 @@ public static class TypeDefinitionExtensions
|
||||
: MethodSignature.CreateInstance(returnType);
|
||||
MethodDefinition result = new MethodDefinition(methodName, methodAttributes, methodSignature);
|
||||
|
||||
result.CilMethodBody = new CilMethodBody(result);
|
||||
result.CilMethodBody = new CilMethodBody();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default constructor for a <see cref="TypeDefinition"/>. Throws an exception if one doesn't exist.
|
||||
/// </summary>
|
||||
public static MethodDefinition GetDefaultConstructor(this TypeDefinition type)
|
||||
{
|
||||
return type.Methods.First(m => m.IsInstanceConstructor() && m.Parameters.Count == 0);
|
||||
}
|
||||
|
||||
public static bool IsStaticClass(this TypeDefinition type)
|
||||
/// <summary>
|
||||
/// Get all the instance constructors for a type
|
||||
/// </summary>
|
||||
/// <param name="type">The definition for the type</param>
|
||||
/// <returns>All the instance constructors</returns>
|
||||
public static IEnumerable<MethodDefinition> GetInstanceConstructors(this TypeDefinition type)
|
||||
{
|
||||
return type.Methods.Where(m => !m.IsStatic && m.IsConstructor);
|
||||
}
|
||||
|
||||
public static bool IsStatic(this TypeDefinition type)
|
||||
{
|
||||
return type.IsSealed && type.IsAbstract;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
|
||||
|
||||
<Version>1.1.6</Version>
|
||||
<Version>1.2.1</Version>
|
||||
<AssemblyVersion>$(Version)</AssemblyVersion>
|
||||
<Copyright>Copyright (c) 2025 ds5678</Copyright>
|
||||
<Authors>ds5678</Authors>
|
||||
|
||||
Reference in new issue
Block a user