mirror of
https://github.com/AssetRipper/AssetRipper.CIL.git
synced 2026-11-13 12:41:25 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
444e6164f6 | ||
|
|
5df204203b | ||
|
|
9fa935cb0f | ||
|
|
f10f09581d | ||
|
|
672c585e52 | ||
|
|
c27bbc1164 | ||
|
|
eda49e0ff3 | ||
|
|
ae137f5afd |
No files matched your search
@@ -9,17 +9,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
|
||||
<PackageReference Include="NUnit" Version="4.4.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
|
||||
<PackageReference Include="NUnit" Version="4.5.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.11.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -37,7 +37,7 @@ public static class CilInstructionCollectionEquality
|
||||
}
|
||||
|
||||
// Instructions
|
||||
return Equals(left.Instructions, right.Instructions);
|
||||
return Equals(left.Instructions, right.Instructions);
|
||||
}
|
||||
|
||||
public static bool Equals(CilExceptionHandler left, CilExceptionHandler right)
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.5" />
|
||||
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.6" />
|
||||
<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.2" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -3,6 +3,7 @@ using AsmResolver.DotNet.Signatures;
|
||||
using AsmResolver.PE.DotNet.Cil;
|
||||
using AsmResolver.PE.DotNet.Metadata.Tables;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
@@ -26,6 +27,10 @@ public static class CilInstructionCollectionExtensions
|
||||
{
|
||||
instructions.AddNullRef();
|
||||
}
|
||||
else if (type is TypeDefOrRefSignature t && TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType))
|
||||
{
|
||||
instructions.AddDefaultPrimitiveValue(underlyingType);
|
||||
}
|
||||
else if (type.IsValueTypeOrGenericParameter())
|
||||
{
|
||||
instructions.AddDefaultValueForUnknownType(type);
|
||||
@@ -48,6 +53,10 @@ public static class CilInstructionCollectionExtensions
|
||||
instructions.AddNullRef();
|
||||
instructions.Add(CilOpCodes.Stloc, localVariable);
|
||||
}
|
||||
else if (type is TypeDefOrRefSignature t && TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType))
|
||||
{
|
||||
instructions.InitializeDefaultPrimitiveValue(underlyingType, localVariable);
|
||||
}
|
||||
else if (type.IsValueTypeOrGenericParameter())
|
||||
{
|
||||
instructions.InitializeDefaultValueForUnknownType(localVariable);
|
||||
@@ -94,11 +103,24 @@ public static class CilInstructionCollectionExtensions
|
||||
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)
|
||||
ElementType.I or ElementType.U => instructions.Add(CilOpCodes.Ldelem_I),
|
||||
ElementType.Object or ElementType.String => instructions.Add(CilOpCodes.Ldelem_Ref),
|
||||
_ => instructions.Add(CilOpCodes.Ldelem, elementType.ToTypeDefOrRef()),
|
||||
},
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Ldelem),
|
||||
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Ldelem) : instructions.Add(CilOpCodes.Ldelem_Ref)
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Ldelem, elementType.ToTypeDefOrRef()),
|
||||
PointerTypeSignature => instructions.Add(CilOpCodes.Ldelem_I),
|
||||
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
|
||||
? instructions.AddLoadElement(underlyingType)
|
||||
: AddUnknown(instructions, t),
|
||||
_ => AddUnknown(instructions, elementType)
|
||||
};
|
||||
|
||||
static CilInstruction AddUnknown(CilInstructionCollection instructions, TypeSignature elementType)
|
||||
{
|
||||
return elementType.IsValueType
|
||||
? instructions.Add(CilOpCodes.Ldelem, elementType.ToTypeDefOrRef())
|
||||
: instructions.Add(CilOpCodes.Ldelem_Ref);
|
||||
}
|
||||
}
|
||||
|
||||
public static CilInstruction AddStoreElement(this CilInstructionCollection instructions, TypeSignature elementType)
|
||||
@@ -118,11 +140,24 @@ public static class CilInstructionCollectionExtensions
|
||||
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),
|
||||
ElementType.I or ElementType.U => instructions.Add(CilOpCodes.Stelem_I),
|
||||
ElementType.Object or ElementType.String => instructions.Add(CilOpCodes.Stelem_Ref),
|
||||
_ => instructions.Add(CilOpCodes.Stelem, elementType.ToTypeDefOrRef()),
|
||||
},
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Stelem),
|
||||
_ => elementType.IsValueType ? instructions.Add(CilOpCodes.Stelem) : instructions.Add(CilOpCodes.Stelem_Ref)
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Stelem, elementType.ToTypeDefOrRef()),
|
||||
PointerTypeSignature => instructions.Add(CilOpCodes.Stelem_I),
|
||||
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
|
||||
? instructions.AddStoreElement(underlyingType)
|
||||
: AddUnknown(instructions, t),
|
||||
_ => AddUnknown(instructions, elementType)
|
||||
};
|
||||
|
||||
static CilInstruction AddUnknown(CilInstructionCollection instructions, TypeSignature elementType)
|
||||
{
|
||||
return elementType.IsValueType
|
||||
? instructions.Add(CilOpCodes.Stelem, elementType.ToTypeDefOrRef())
|
||||
: instructions.Add(CilOpCodes.Stelem_Ref);
|
||||
}
|
||||
}
|
||||
|
||||
public static CilInstruction AddLoadIndirect(this CilInstructionCollection instructions, TypeSignature type)
|
||||
@@ -146,7 +181,9 @@ public static class CilInstructionCollectionExtensions
|
||||
},
|
||||
PointerTypeSignature => instructions.Add(CilOpCodes.Ldind_I),
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Ldobj, type.ToTypeDefOrRef()),
|
||||
TypeDefOrRefSignature t => AddTypeDefOrRef(instructions, t),
|
||||
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
|
||||
? instructions.AddLoadIndirect(underlyingType)
|
||||
: AddUnknown(instructions, type),
|
||||
_ => AddUnknown(instructions, type),
|
||||
};
|
||||
|
||||
@@ -156,15 +193,6 @@ public static class CilInstructionCollectionExtensions
|
||||
? instructions.Add(CilOpCodes.Ldobj, type.ToTypeDefOrRef())
|
||||
: instructions.Add(CilOpCodes.Ldind_Ref);
|
||||
}
|
||||
|
||||
static CilInstruction AddTypeDefOrRef(CilInstructionCollection instructions, TypeDefOrRefSignature type)
|
||||
{
|
||||
// Check if the type is an enum
|
||||
TypeSignature underlyingType = type.GetUnderlyingType();
|
||||
return underlyingType is CorLibTypeSignature
|
||||
? instructions.AddLoadIndirect(underlyingType)
|
||||
: AddUnknown(instructions, underlyingType);
|
||||
}
|
||||
}
|
||||
|
||||
public static CilInstruction AddStoreIndirect(this CilInstructionCollection instructions, TypeSignature type)
|
||||
@@ -188,7 +216,9 @@ public static class CilInstructionCollectionExtensions
|
||||
},
|
||||
PointerTypeSignature => instructions.Add(CilOpCodes.Stind_I),
|
||||
GenericParameterSignature => instructions.Add(CilOpCodes.Stobj, type.ToTypeDefOrRef()),
|
||||
TypeDefOrRefSignature t => AddTypeDefOrRef(instructions, t),
|
||||
TypeDefOrRefSignature t => TryGetEnumUnderlyingType(instructions, t, out CorLibTypeSignature? underlyingType)
|
||||
? instructions.AddStoreIndirect(underlyingType)
|
||||
: AddUnknown(instructions, t),
|
||||
_ => AddUnknown(instructions, type),
|
||||
};
|
||||
|
||||
@@ -198,15 +228,6 @@ public static class CilInstructionCollectionExtensions
|
||||
? instructions.Add(CilOpCodes.Stobj, type.ToTypeDefOrRef())
|
||||
: instructions.Add(CilOpCodes.Stind_Ref);
|
||||
}
|
||||
|
||||
static CilInstruction AddTypeDefOrRef(CilInstructionCollection instructions, TypeDefOrRefSignature type)
|
||||
{
|
||||
// Check if the type is an enum
|
||||
TypeSignature underlyingType = type.GetUnderlyingType();
|
||||
return underlyingType is CorLibTypeSignature
|
||||
? instructions.AddStoreIndirect(underlyingType)
|
||||
: AddUnknown(instructions, underlyingType);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -337,4 +358,10 @@ public static class CilInstructionCollectionExtensions
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryGetEnumUnderlyingType(CilInstructionCollection instructions, TypeDefOrRefSignature type, [NotNullWhen(true)] out CorLibTypeSignature? underlyingType)
|
||||
{
|
||||
underlyingType = type.GetUnderlyingType(instructions.Owner.Owner?.DeclaringModule?.RuntimeContext) as CorLibTypeSignature;
|
||||
return underlyingType is not null;
|
||||
}
|
||||
}
|
||||
@@ -35,12 +35,19 @@ public static class EventDefinitionExtensions
|
||||
|
||||
public static bool HasBackingField(this EventDefinition eventDefinition, [NotNullWhen(true)] out FieldDefinition? backingField)
|
||||
{
|
||||
TypeSignature? eventType = eventDefinition.EventType?.ToTypeSignature();
|
||||
RuntimeContext? runtimeContext = eventDefinition.DeclaringModule?.RuntimeContext;
|
||||
if (runtimeContext is null)
|
||||
{
|
||||
backingField = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
TypeSignature? eventType = eventDefinition.EventType?.ToTypeSignature(runtimeContext);
|
||||
if (eventType is not null)
|
||||
{
|
||||
foreach (FieldDefinition field in eventDefinition.DeclaringType?.Fields ?? [])
|
||||
{
|
||||
if (field.IsPrivate && field.Name == eventDefinition.Name && SignatureComparer.Default.Equals(field.Signature?.FieldType, eventType))
|
||||
if (field.IsPrivate && field.Name == eventDefinition.Name && runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, eventType))
|
||||
{
|
||||
backingField = field;
|
||||
return true;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
@@ -33,7 +32,8 @@ public static class FieldDefinitionExtensions
|
||||
property = field.DeclaringType?.Properties.FirstOrDefault(p => p.Name == propertyName);
|
||||
return property is not null
|
||||
&& field.IsStatic == property.IsStatic()
|
||||
&& SignatureComparer.Default.Equals(field.Signature?.FieldType, property.Signature?.ReturnType);
|
||||
&& field.DeclaringModule?.RuntimeContext is { } runtimeContext
|
||||
&& runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, property.Signature?.ReturnType);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -49,7 +49,8 @@ public static class FieldDefinitionExtensions
|
||||
@event = field.DeclaringType?.Events.FirstOrDefault(e => e.Name == field.Name);
|
||||
return @event is not null
|
||||
&& field.IsStatic == @event.IsStatic()
|
||||
&& SignatureComparer.Default.Equals(@event.EventType?.ToTypeSignature(), field.Signature?.FieldType);
|
||||
&& @event.DeclaringModule?.RuntimeContext is { } runtimeContext
|
||||
&& runtimeContext.SignatureComparer.Equals(@event.EventType?.ToTypeSignature(runtimeContext), field.Signature?.FieldType);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
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;
|
||||
|
||||
@@ -114,7 +113,8 @@ public static class MethodDefinitionExtensions
|
||||
if (field is null
|
||||
|| !field.IsPrivate
|
||||
|| field.IsStatic != method.IsStatic
|
||||
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType))
|
||||
|| method.DeclaringModule?.RuntimeContext is not { } runtimeContext
|
||||
|| !runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, method.Signature?.ReturnType))
|
||||
{
|
||||
backingField = null;
|
||||
return false;
|
||||
@@ -144,7 +144,8 @@ public static class MethodDefinitionExtensions
|
||||
if (field is null
|
||||
|| !field.IsPrivate
|
||||
|| field.IsStatic != method.IsStatic
|
||||
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType))
|
||||
|| method.DeclaringModule?.RuntimeContext is not { } runtimeContext
|
||||
|| !runtimeContext.SignatureComparer.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType))
|
||||
{
|
||||
backingField = null;
|
||||
return false;
|
||||
|
||||
@@ -118,6 +118,11 @@ internal static class MethodStubber
|
||||
{
|
||||
return null;
|
||||
}
|
||||
RuntimeContext? runtimeContext = methodDefinition.DeclaringModule?.RuntimeContext;
|
||||
if (runtimeContext is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (baseType is TypeDefinition baseTypeDef)
|
||||
{
|
||||
@@ -125,8 +130,8 @@ internal static class MethodStubber
|
||||
}
|
||||
else if (baseType is TypeReference baseTypeRef)
|
||||
{
|
||||
TypeDefinition? baseTypeResolved = baseTypeRef.Resolve();
|
||||
if (baseTypeResolved is null)
|
||||
ResolutionStatus resolutionStatus = baseTypeRef.Resolve(runtimeContext, out TypeDefinition? baseTypeResolved);
|
||||
if (baseTypeResolved is null || resolutionStatus is not ResolutionStatus.Success)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -147,8 +152,8 @@ internal static class MethodStubber
|
||||
{
|
||||
return null;
|
||||
}
|
||||
TypeDefinition? baseTypeResolved = genericInstanceTypeSignature.GenericType.Resolve();
|
||||
if (baseTypeResolved is null)
|
||||
ResolutionStatus resolutionStatus = genericInstanceTypeSignature.GenericType.Resolve(runtimeContext, out TypeDefinition? baseTypeResolved);
|
||||
if (baseTypeResolved is null || resolutionStatus is not ResolutionStatus.Success)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -165,7 +170,7 @@ internal static class MethodStubber
|
||||
|
||||
static MethodDefinition? GetFirstCompatibleConstructor(TypeDefinition declaringType, TypeDefinition baseType)
|
||||
{
|
||||
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && m.IsAccessibleFromType(declaringType));
|
||||
return baseType.Methods.FirstOrDefault(m => m.IsInstanceConstructor() && m.IsAccessibleFromType(declaringType, m.DeclaringModule?.RuntimeContext));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,10 +187,13 @@ internal static class MethodStubber
|
||||
return field;
|
||||
}
|
||||
|
||||
TypeSignature[] typeArguments = Enumerable.Range(0, field.DeclaringType.GenericParameters.Count)
|
||||
.Select(i => new GenericParameterSignature(field.DeclaringModule, GenericParameterType.Type, i))
|
||||
.ToArray();
|
||||
int count = field.DeclaringType.GenericParameters.Count;
|
||||
TypeSignature[] typeArguments = new TypeSignature[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
typeArguments[i] = new GenericParameterSignature(field.DeclaringModule, GenericParameterType.Type, i);
|
||||
}
|
||||
|
||||
return new MemberReference(field.DeclaringType.MakeGenericInstanceType(typeArguments).ToTypeDefOrRef(), field.Name, field.Signature);
|
||||
return new MemberReference(field.DeclaringType.MakeGenericInstanceType(field.DeclaringModule?.RuntimeContext, typeArguments).ToTypeDefOrRef(), field.Name, field.Signature);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using AsmResolver.DotNet;
|
||||
using AsmResolver.DotNet.Signatures;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace AssetRipper.CIL;
|
||||
@@ -13,7 +12,8 @@ public static class PropertyDefinitionExtensions
|
||||
if (backingField is null
|
||||
|| !backingField.IsPrivate
|
||||
|| backingField.IsStatic != property.IsStatic()
|
||||
|| !SignatureComparer.Default.Equals(backingField.Signature?.FieldType, property.Signature?.ReturnType))
|
||||
|| backingField.DeclaringModule?.RuntimeContext is not { } runtimeContext
|
||||
|| !runtimeContext.SignatureComparer.Equals(backingField.Signature?.FieldType, property.Signature?.ReturnType))
|
||||
{
|
||||
backingField = null;
|
||||
return false;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
|
||||
|
||||
<VersionPrefix>1.2.2</VersionPrefix>
|
||||
<VersionPrefix>1.3.0</VersionPrefix>
|
||||
<Copyright>Copyright (c) 2025 ds5678</Copyright>
|
||||
<Authors>ds5678</Authors>
|
||||
<Company>AssetRipper</Company>
|
||||
|
||||
Reference in new issue
Block a user